I have a sample JSON file like this
data = {
"type": "video",
"videoID": "vid001",
"links": [
{"type": "video", "videoID": "vid002", "links": []},
{"type": "video",
"videoID": "vid003",
"links": [
{"type": "video", "videoID": "vid004"},
{"type": "video", "videoID": "vid005"},
]
},
{"type": "video", "videoID": "vid006"},
{"type": "video",
"videoID": "vid007",
"links": [
{"type": "video", "videoID": "vid008", "links": [
{"type": "video",
"videoID": "vid009",
"links": [{"type": "video", "videoID": "vid010"}]
}
]}
]},
]
}
i need to take only specific key and values from the json file and convert it into a CSV file
Code: Ref:Extracting Specific Keys/Values From A Messed-Up JSON File (Python)
def extract(data, keys):
out = []
queue = [data]
while len(queue) > 0:
current = queue.pop(0)
if type(current) == dict:
for key in keys:
if key in current:
out.append({key: current[key]})
for val in current.values():
if type(val) in [list, dict]:
queue.append(val)
elif type(current) == list:
queue.extend(current)
return out
x = extract(data, ["videoID","type"])
print(pd.DataFrame.from_dict(x))
When I'm passing 2 values through the extract(), getting NaN in between result
videoID type
0 vid001 NaN
1 NaN video
2 vid002 NaN
3 NaN video
4 vid003 NaN
5 NaN video
6 vid006 NaN
7 NaN video
8 vid007 NaN
9 NaN video
10 vid004 NaN
11 NaN video
12 vid005 NaN
13 NaN video
14 vid008 NaN
15 NaN video
16 vid009 NaN
17 NaN video
18 vid010 NaN
19 NaN video
I need to get an output like the below
videoID type
0 vid001 video
1 vid002 video
2 vid003 video
3 vid004 video
etc...
and convert it into a csv file, can somebody help me to solve this issue