skip to Main Content

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


  1. 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

    import json
    
    in_file = open("/Input/path/to/sample.json", "r")
    
    out_file = open("/Output/path/to/converted_json.json", "w")
    
    input_json = json.load(in_file)
    print(input_json)
    
    converted_json = {"names": [input_json[key] for key in input_json]}
    print(converted_json)
    
    json.dump(converted_json, out_file)
    
    in_file.close(), out_file.close()
    

    Out put will be a json file with name converetd_json.json with below content.

    {
    "names": [{
            "name": "xyz",
            "roll": "123",
            "id": "A1"
        },
        {
            "name": "abc",
            "roll": "456",
            "id": "A2"
        },
        {
            "name": "def",
            "roll": "789",
            "id": "B1"
        }
       ]
    }
    

    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

    import json
    
    in_file = open("/Input/path/to/sample.json", "r")
    
    out_file = open("/Output/path/to/converted_json.json", "w")
    
    input_json = json.load(in_file)
    print(input_json)
    
    converted_json = [input_json[key] for key in input_json]
    print(converted_json)
    
    json.dump(converted_json, out_file)
    
    in_file.close(), out_file.close()
    

    Out put will be a json file with name converetd_json.json with below content.

    [
      {
        "name": "xyz",
        "roll": "123",
        "id": "A1"
      },
      {
        "name": "abc",
        "roll": "456",
        "id": "A2"
      },
      {
        "name": "def",
        "roll": "789",
        "id": "B1"
       }
    ]
    

    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.

    Login or Signup to reply.
  2. 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)
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search