Home > Net >  While trying to print the json data in jupyter notebook, I am getting JSON decode error?
While trying to print the json data in jupyter notebook, I am getting JSON decode error?

Time:01-11

I am trying to print JSON data in jupyter notebook, But I am getting the jsondecodeerror.

Code

import json
people_string ='''
{
"people":[
"name":"John Smith",
"phone":"615-555-7164",
"emails":["[email protected]","[email protected]"],
"has_license":false
},
{
"name":"Jane Doe",
"phone":"560-555-5153",
"emails":null,
"has_license":true

}
]  
}

data=json.loads(people_string)
print(data)

CodePudding user response:

There is a missing opening { in the first element of people.

{ 
   "people":[
      {
          "name":"John Smith",
          "phone":"615-555-7164",
          "emails": [
              "[email protected]",
              "[email protected]"
          ], 
          "has_license":false
      }, { 
          "name":"Jane Doe",
          "phone":"560-555-5153", 
          "emails":null,
          "has_license":true
       }
   ]  
}

CodePudding user response:

There are syntax errors in your Json, here is the version correctly formatted:

import json

people_string = '''
{
    "people": [
        {
            "name": "John Smith",
            "phone": "615-555-7164",
            "emails": [
                "[email protected]",
                "[email protected]"
            ],
            "has_license": false
        },
        {
            "name": "Jane Doe",
            "phone": "560-555-5153",
            "emails": null,
            "has_license": true
        }
    ]
}
'''

data = json.loads(people_string)
print(data)

CodePudding user response:

For debugging purposes, you can use a JSON viewer such as this one to validate your JSON and also to visualize it a little better.

Pasting the JSON above I run into the following issue which gets highlighted:

Parse error on line 3:
{"people":["name":"John Smith""phone
-----------------^
Expecting 'EOF', '}', ',', ']', got ':'

The fix seems straightforward enough: you can add a { - open brace to signal the start of a map object - after the initial start of the list at [. Once I did this, the JSON seems to be valid and am able to load and visualize the data as expected.

  •  Tags:  
  • Related