I have a json file which looks like this
{
"name1": {
"name": "xyz",
"roll": "123",
"id": "A1"
},
"name2":{
"name": "abc",
"roll": "456",
"id": "A2"
},
"name3":{
"name": "def",
"roll": "789",
"id": "B1"
}
}
I want to remove name1
, name2
, name3
and should be looking like this
{
{
"name": "xyz",
"roll": "123",
"id": "A1"
},
{
"name": "abc",
"roll": "456",
"id": "A2"
},
{
"name": "def",
"roll": "789",
"id": "B1"
}
}
Is there any way to do this in python? If this problem is asked earlier then me please refer to one cause I can't find it. Help will be thankful.
CodePudding user response:
Based on you're example, you can actually remove elements and still make a valid json. Like i commented above, you just need to convert them into array
[
{
"name": "xyz",
"roll": "123",
"id": "A1"
},
{
"name": "abc",
"roll": "456",
"id": "A2"
},
{
"name": "def",
"roll": "789",
"id": "B1"
}
]
CodePudding user response:
You can use simple python script. First, you need to read the json file and then load it as an json object. Then, you can iterate on the whole json object to change it to whatever format you need.
import json
with open("file.json","r ") as file:
s = file.read()
jfile = json.loads(s)
with open("ss.json","w") as file:
json.dump([jfile[i] for i in jfile], file)