Hi, i’m trying to convert JSON to CSV. So far i have the following code, which i mostly found here.
#! python3
# r: numpy, pandas, json
import rhinoscriptsyntax as rs
import pandas as pd
import json as json
# Reading JSON data from a file
with open("data.json") as f:
json_data = json.load(f)
# Converting JSON data to a pandas DataFrame
df = pd.read_json(json_data)
# Writing DataFrame to a CSV file
df.to_csv("output.csv", index=False)
It throws up the error:
Error building code | Error installing: json | ERROR: Could not find a version that satisfies the requirement json (from versions: none)ERROR: No matching distribution found for json
Tried switching load.json to json.load, which seemed logical to me but i’m a novice and it didn’t work, so probably that wasn’t it.
I have not had a chance to test this. But it looks like JSON is not on PyPi.
There are other JSON libraries there, so using another name might work.
The second way is to download the JSON library and put it in a folder. The # env the folder location at the top of the script. This does best withe the 8.6 release candidate build.
Error building code | Error installing: json | ERROR: Could not find a version that satisfies the requirement json (from versions: none)ERROR: No matching distribution found for json```
Which seems to confirm what you said.
Will browse through the library and try a few. Also found json2csv, which sounds like what i need.
#! python3
# r: numpy, pandas
import rhinoscriptsyntax as rs
import pandas as pd
import json
# Reading JSON data from a file
with open("/Users/bas/Downloads/data.json") as f:
json_data = json.load(f)
# Converting JSON data to a pandas DataFrame
df = pd.read_json(json_data)
# Writing DataFrame to a CSV file
df.to_csv("output.csv", index=False)
This was on me. There are two *.json on the page you linked and i the wrong one. Try this one. Worked for me
#! python3
# r: numpy, pandas
import rhinoscriptsyntax as rs
import pandas as pd
import json
# Reading JSON data from a file
with open("/Users/bas/Downloads/data.json") as f:
json_data = json.load(f)
# Converting JSON data to a pandas DataFrame
df = pd.json_normalize(json_data)
# Writing DataFrame to a CSV file
df.to_csv("flattened_output.csv", index=False)
Thanks Dan, that seems to work for me as well. Just realized a big mistake on my side though, as i’m trying to stream the json data directly within GH, and output it also directly as CSV code. So just reformatting the data. Without any files, which might come later. That is why i was stuck data.json… been a while since i tried programming
John, that might also be the reason why JSON was acting up? Couldn’t also find that section you are showing me. If you’re still interested let me know, i’m on a mac though.
However, json did not need to be added via # r: .... I removed it in my version of the code, as it is a standard module, which did not need this in Python 2.
Hi Dan, i’m now trying to achieve reformatting without writing/reading any intermediate files. Do you know how to do that? Is the json_normalize what i need for that?
Been trying this, but it appears its not that simple:
#! python3
# r: numpy, pandas
import rhinoscriptsyntax as rs
import pandas as pd
import json
data_json = data
data_json_flatten = pd.json_normalize(data_json)
data_json_flatten.to_csv(csv_data, index=False)
The error i get is this:
Traceback (most recent call last):
File "rhinocode:///grasshopper/1/f83bb4a8-88f5-4a2b-b445-f5ea5fc0d69d/e01098a1-f939-416b-8db3-00394c91cb55", line 9, in <module>
File "/Users/bas/.rhinocode/py39-rh8/site-envs/default-k0rnaTm8/pandas/io/json/_normalize.py", line 445, in json_normalize
raise NotImplementedError
NotImplementedError: