0

I have a sample JSON in this format:

JSON FILE:

{
   "Name": "ABC",
   "Phone":"123",
   "Address":[{"City":"City-1"},{"Country":"Country-1"}]
}

{
   "Name": "ABC-1",
   "Phone":"123-1",
   "Address":[{"City":"City-2"},{"Country":"Country-2"}]
}

Is there any approach to parse the JSON and loop through the file and print each key-value pair.

The approach I used was through using

json_open = open(json_file)
json_data = json.load(json_open)
print(json_data[Name]) ##should give ABC
print(json_data[Name]) ##should give ABC-1 - unsure about the syntax and format

But I'm currently able to print only the first object values - i.e. name=ABC and not name=ABC-1

FirstName
  • 377
  • 2
  • 6
  • 21
  • 1
    Can you add expected output too for the sample provided? – Gurupad Hegde Jul 05 '16 at 04:45
  • Is this a valid json ? It should be only one dictionary not multiple dictionaries. – Pratik Gujarathi Jul 05 '16 at 05:25
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – outis May 20 '21 at 05:07

3 Answers3

1

There is error in your json file. I modified your json and written code for traverse each element in it.

Error:

Error: Parse error on line 9:
... "Country-1" }]}{    "Name": "ABC-1",
-------------------^
Expecting 'EOF', '}', ',', ']', got '{'

sample.json

{
  "data": [
    {
      "Name": "ABC",
      "Phone": "123",
      "Address": [
        {
          "City": "City-1"
        },
        {
          "Country": "Country-1"
        }
      ]
    },
    {
      "Name": "ABC-1",
      "Phone": "123-1",
      "Address": [
        {
          "City": "City-2"
        },
        {
          "Country": "Country-2"
        }
      ]
    }
  ]
}

sample.py

import json

json_file='sample.json'
with open(json_file, 'r') as json_data:
    data = json.load(json_data)

jin=data['data']

for emp in jin:
      print ("Name :"+emp["Name"])
      print ("Phone :"+emp["Phone"])
      print ("City :"+emp["Address"][0]["City"])
      print ("Country :"+emp["Address"][1]["Country"])
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
  • I see that you've encountered an error while parsing the json, but a json can exist in this format - `{ Key:value, Key:value } { Key:value, Key:value }` I'm trying to parse a json of this type, rather than converting it to array as you've done in your modified json file. – FirstName Jul 05 '16 at 05:44
  • Each record on json file is separated by ','. So, your file should look like: ` [{ "Name": "ABC", "Phone": "123", "Address": [{ "City": "City-1" }, { "Country": "Country-1" }] }, { "Name": "ABC-1", "Phone": "123-1", "Address": [{ "City": "City-2" }, { "Country": "Country-2" }] } ]` – Sijan Bhandari Jul 05 '16 at 05:44
0

Each record on json file is separated by ','. So, your file should look like:

[{
    "Name": "ABC",
    "Phone": "123",
    "Address": [{
        "City": "City-1"
    }, {
        "Country": "Country-1"
    }]
},

{
    "Name": "ABC-1",
    "Phone": "123-1",
    "Address": [{
        "City": "City-2"
    }, {
        "Country": "Country-2"
    }]
}
]

You can read the file and output as follows :

import json

my_file='test.json'
with open(my_file, 'r') as  my_data:
    data = json.load(my_data)

print data

for elm in data:
    print elm['Phone']
    print elm['Name']
    print elm['Address'][0]['City']
    print elm['Address'][1]['Country']  
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36
0

First of all this is not valid json format,

You can check here you json format

You should use this try.json file

{
    "data":[{
        "Name": "ABC",
        "Phone":"123",
        "Address":[{"City":"City-1"},{"Country":"Country-1"}]
        }, {
        "Name": "ABC-1",
        "Phone":"123-1",
        "Address":[{"City":"City-2"},{"Country":"Country-2"}]
    }]
}

here is try.py

import json
json_open = open("try.json")
json_data = json.load(json_open)
for i in range(len(json_data['data'])):
    print(json_data['data'][i]["Name"])

Note : python code is written in python3

output is look line this

ABC
ABC-1