问题
Here is a template of my JSON:
{
"field 1": [
{
"id": "123456"
},
{
"about": "YESH"
},
{
"can_post": true
},
{
"category": "Community"
}
],
"field 2": [
{
"id": "123456"
},
{
"about": "YESH"
},
{
"can_post": true
},
{
"category": "Community"
}
]
}
I would like to convert this JSON into a csv in the following format using Python:
0 field 1, id, about, can_post, category
1 field 2, id, about, can_post, category
I tried using pandas to read_json and then to_csv but it didn't work.
Thanks
回答1:
import csv
import json
json.load( json_data) Deserialize the json_data ( json document(txt/ binary file)) to python object.
with open('jsn.txt','r') as json_data:
json_dict = json.load(json_data)
since your field names( keys that will act as fieldname) are inside different dicts, we have to go over this dicts and put them in list field_names.
field_names = [ 'field']
for d in json_dict['field 1']:
field_names.extend(d.keys())
with open('mycsvfile.csv', 'w') as f:
w = csv.DictWriter(f, fieldnames = fieild_names)
w.writeheader()
for k1, arr_v in json_dict.items():
temp = {k2:v for d in arr_v for k2,v in d.items()}
temp['field'] = k1
w.writerow(temp)
Output
field,id,about,can_post,category
field 1,123456,YESH,True,Community
field 2,123456,YESH,True,Community
If you find above dict comprehension confusing
k1 : arr_v
'field 1' = [{ "id": "123456" },...{"category": "Community"}]
for d in arr_v:
k2 : v
d --> { "id": "123456" }
回答2:
how about this, if you have json like data
data = [
{
"site": "field1",
"id": "123456",
"about": "YESH",
"can_post": True,
"category": "Community"
},
{
"site": "field2",
"id": "123456",
"about": "YESH",
"can_post": True,
"category": "Community"
}
]
# also use True instead of true
df = pd.DataFrame.from_dict(data)
print(df)
# use df.to_csv('filename.csv') for csv
output:
about can_post category id site
0 YESH True Community 123456 field1
1 YESH True Community 123456 field2
回答3:
The hard part here is that you json initial structure is not simply a list of mappings but a mapping where the values are in turn lists of mappings.
IMHO, you have to pre-process your input, or process it element by element to get a list or a mapping that can be converted to a csv row. Here is a possible solution that:
- extract the keys for the first element and use them to build a DictWriter
- build a mapping for every element and store it in the DictWriter
Code could be:
import json
import csv
# read the json data
with open("input.json") as fd:
data = json.load(fd)
# extract the field names (using 'field' for the key):
names = ['field']
for d in next(iter(data.values())):
names.extend(d.keys())
# open the csv file as a DictWriter using those names
with open("output.csv", "w", newline='') as fd:
wr = csv.DictWriter(fd, names)
wr.writeheader()
for field, vals in data.items():
d['field'] = field
for inner in vals:
for k,v in inner.items():
d[k] = v
wr.writerow(d)
With your data it gives:
field,id,about,can_post,category
field 1,123456,YESH,True,Community
field 2,123456,YESH,True,Community
来源:https://stackoverflow.com/questions/52756660/converting-a-json-with-a-nested-array-to-csv