Getting specific key, value pairs from JSON and put them into a new JSON file

Viewed 26

I have a big json file and I would like to get specific "key"="value" pairs from the file and put those into a new json file. So my final json file only have desired "key,value" pairs.

What I managed so far is creating a loop to list key name and the corresponding output as below:

madde  dulda
madde  dulavrat
.
.
.
madde   zam

My JSON (just a part of it)

[
{"_id":1,
"madde_id":"1",
"kac":"0",
"kelime_no":"14800",
"cesit":"0",
"anlam_gor":"0",
"on_taki":null,
"madde":"dulavrat otu",
"cesit_say":"0",
"anlam_say":"1",
"taki":null,
"cogul_mu":"0",
"ozel_mi":"0",
"lisan_kodu":"0",
"lisan":"",
"telaffuz":null,
"birlesikler":null,
"font":null,
"madde_duz":"dulavrat otu",
"gosterim_tarihi":null,

"anlamlarListe":[
{"anlam_id":"25840","madde_id":"1","anlam_sira":"1","fiil":"0","tipkes":"0","anlam":"Birleşikgillerden, hekimlikte kullanılan bir bitki (Arctium tomentosum)","gos":"0",

"ozelliklerListe":[{"ozellik_id":"19","tur":"3","tam_adi":"isim","kisa_adi":"a.","ekno":"30"},
{"ozellik_id":"41","tur":"1","tam_adi":"bitki bilimi","kisa_adi":"bit. b.","ekno":"90"}]}]}
]

My code is:

for i in data:
   print('madde', i['madde'])
1 Answers

I found the solution:

import json

with open('C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Python39_64\gts5copy.json',encoding="utf8") as f:
    
data = json.load(f)

getdata = []
for each in data:
   d = {'madde': each['madde']}
   getdata.append(d)

with open('c:\\temp\\outputfile.json','w', encoding='utf-8') as f:
   json.dump(getdata,f,ensure_ascii=False)
Related