The reason for this is that i got some code, which represents buildingparts for an LCA calculation. Instead of creating all these buildingparts from scratch in the script i would like to pair the allready existing library. This way i want to fetch the specific code parts to single out my choosen building parts. I’m using Jswan pluging to extract the different parameters, that i think i might be able to sort/extract parts of the code, in regards of specific values. Needed.json (57.4 KB) Example.json (431 Bytes) Grasshopper_Read json into grasshopper.gh (11.0 KB)
I think the JSON file has been read in just fine. Can you really not live without printing the whole thing to a text panel? How about just printing an extract?
My problem is that, if i cannot print it, i can’t use the Jswan component. They will only run, if it’s in a panel or able to be in a panel.
Sadly, i’m i don’t know how to python (not experienced in python at all) my way out of extraction parts of the json file, thorugh python coding only. Therefore i would like to use i Jswan components, where i’m able to single out specific nodes and edges.
Getting a good JSON reader may help. There are a number of free ones. They can help with node levels and names.
Also there is a large change in the node structure after the 30th member in the JSON. That needs to also be watched. The loop below just protects against that.
Here is a reading using Rhino 8 and the Python component
Or here is a walk thru the values:
import rhinoscriptsyntax as rs
import json
#Read JSON data into the datastore variable
if file:
with open(file, 'r') as f:
datastore = json.load(f)
a = []
#Use the new datastore datastructure
for i in range(len(datastore)):
if i < 30:
a.append(datastore[i]["Node"]["Element"]["name"]["Danish"])
The edges were mentioned. Here is a way to get out the edge arrays:
import rhinoscriptsyntax as rs
import json
#Read JSON data into the datastore variable
if file:
with open(file, 'r') as f:
datastore = json.load(f)
edge1 = []
edge2 = []
edge3 = []
#Use the new datastore datastructure
for i in range(len(datastore)):
if i > 29:
edge1.append(datastore[i]["Edge"][0]['ElementToConstruction']['id'])
edge2.append(datastore[i]["Edge"][0]['ElementToConstruction']['id'])
edge3.append(datastore[i]["Edge"][0]['ElementToConstruction']['id'])
I know that there probably is a way to make an input in the python script to retrieve the information that i retrieve by the list items… example: by searching for the [“Node”][“Element”][“name”][“Danish”]
But i think i really need to look into some basic python / Json tutorials to learn this. but this is so great!
my (slightly) modified python:
import rhinoscriptsyntax as rs
import json
#Read JSON data into the datastore variable
if file:
with open(file, 'r') as f:
datastore = json.load(f)
a = []
b = []
c = []
#Use the new datastore datastructure
for i in range(len(datastore)):
if i < 30:
a.append(datastore[i]["Node"]["Construction"]["name"]["Danish"])
b.append(datastore[i]["Node"]["Construction"]["id"])
c.append(datastore[i]["Node"]["Construction"]["unit"])
Looks like you are well on your way here. The JSON file is read into a Python Dictionary. So manipulating and walking around the data is learning to use dictionaries.
There is a lot of information on the web on many ways to work with dictionaries.