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.
2
Answers
Since you’re expecting the outermost (root) level of json document to be an object (curly braces) you should specify at least one key to start with. Refer below example.
Python code Example
Out put will be a json file with name
converetd_json.json
with below content.If you want to avoid this root key, an alternative is to use an
array
(Square Brackets) as an outermost level. Refer below example.Python Code
Out put will be a json file with name
converetd_json.json
with below content.Note: The outermost level of a JSON document is either an "object" (curly braces) or an "array" (square brackets). So in this case both are valid json.
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.