Converting JSON to CSV

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:

  1. 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.

What am i doing wrong? Please advice

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.

I will give it a try later this morning.

Thanks Scott. Sometimes i get 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```

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.

It is not in PyPI as json is a standard python module

Maybe worth a try removing json from # r:

1 Like

Tried that, as well as remove “as json”. But still

1. Error running script: [Errno 2] No such file or directory: 'json.data' [9:1]

This is a new error, which is already a progress.

Where is data.json located? Try an absolute path like r"C:/Users/.../data.json"

What version of rhino 8 is running? Syseditncommand will report that in Rhno. The newest rhino 8.5 or 8.6 has to module loading.

Hmm, quick search gave no results and i have no idea where to look tbh. i’m on a mac though… perhaps that explains something.

Oh! I’m on 8.4… installed last week so thought i would be up to date. I’ll update it.

Here is the file, download it and grab the location from properties, then replace data.json with the full path

data.json (170 Bytes)

Thanks! Think i got that working.

Code now:

#! 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)

But now i get

  1. Error running script: Invalid file path or buffer object type: <class ‘list’> [13:1]

This is after “df = pd.read_json(json_data)”

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)

1 Like

Can you go to Tools > Advanced > Open language Status Panel. Still trying to figure out why JSON needed to be added?

image

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 :sweat_smile:

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.

I cannot find Advanced via Tools.

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: 

Sorry, I was not clear. Tools Pulldown in ScriptEditor:

image

Seems like that’s not on mac?

Note, that Python 3.9 was used inside Grasshopper.

If there is a new enough Rhino 8, it should be here: