简体   繁体   中英

How do I manipulate key, value pairs in JSON file?

I have a JSON file that presents information like this for nearly 250 datapoints.

{'China': {'Content' : ['china', 'country', 'south', 'east', 'asia', 'most', 'populous']}}

I want it to become like this.

{'Title': 'China', 
'Content':['china', 'country', 'south', 'east', 'asia', 'most', 'populous']}

How do I do that?

I have already tried Dataframe methods but couldn't figure out.

How do I manipulate the key, value pairs in JSON file?

Try this:

my_dict = {'China': {'Content' : ['china', 'country', 'south', 'east', 'asia', 'most', 'populous']}}
result= []
for k,v in my_dict.items():
    result.append({'title':k, 'Content':v['Content']})

Maybe the below code can help,

present_dict = {'China': {'Content' : ['china', 'country', 'south', 'east', 'asia', 'most', 'populous']}}
new_dict = dict()

for i,k in present_dict.items():
    new_dict['Title'] = i
    new_dict[next(iter(k))] = next(iter(k.values()))

print(new_dict)

output: {'Title': 'China', 'Content': ['china', 'country', 'south', 'east', 'asia', 'most', 'populous']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM