I have a JSON file with the following data:
[{ "planet": "project pluto", "records": [ { "project": "project pluto", "plan": "paper", "start": 1, "stop": 2 } ] }, { "planet": "project venus", "records": [ { "project": "project venus", "plan": "rock", "start": 3, "stop": 4 } ] }, { "planet": "project earth", "records": [ { "project": "project earth", "plan": "scissors", "start": 5, "stop": 6 } ] } ]
Whereby I want my output to be as follows:
[{'planet': 'project pluto', 'records': [['project pluto', 'paper', 1, 2]]}, {'planet': 'project venus', 'records': [{'project venus','rock', 3,4}]}, {'planet': 'project earth', 'records': [{'project earth', 'scissors',5, 6}]}]
This is the code that I currently have, however it only works for the first part of the JSON file and does not iterate over all the other values:
import json with open('planets.json', 'r') as f: data = json.loads(f.read()) data[0]['records'][0] = list(data[0]['records'][0].values()) print(data)
When I do run the above code the output is as follows:
[{'planet': 'project pluto', 'records': [['project pluto', 'paper', 1, 2]]}, {'planet': 'project venus', 'records': [{'project': 'project venus', 'plan': 'rock', 'start': 3, 'stop': 4}]}, {'planet': 'project earth', 'records': [{'project': 'project earth', 'plan': 'scissors', 'start': 5, 'stop': 6}]}]
Question is: How can I iterate and apply to all the values in the JSON file?
Advertisement
Answer
You can do it by iterating through the 'records'
of each object in the data
array.
Here’s how to do it “in-place” (i.e. it modifies data
itself):
import json from pprint import pprint with open('planets.json', 'r') as f: data = json.loads(f.read()) for obj in data: obj['records'] = list(obj['records'][0].values()) pprint(data, sort_dicts=False)
Output:
[{'planet': 'project pluto', 'records': ['project pluto', 'paper', 1, 2]}, {'planet': 'project venus', 'records': ['project venus', 'rock', 3, 4]}, {'planet': 'project earth', 'records': ['project earth', 'scissors', 5, 6]}]
If you don’t want to modify the original data
array, you would need to do something along these lines:
from copy import deepcopy import json from pprint import pprint with open('planets.json', 'r') as f: data = json.loads(f.read()) modified_data = [] for obj in data: new_obj = {key: deepcopy(value) for key, value in obj.items() if key != 'records'} new_obj['records'] = [deepcopy(value) for value in obj['records'][0].values()] modified_data.append(new_obj) pprint(modified_data, sort_dicts=False)