Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON with python: blank fields

I'm having problems while parsing a JSON with python, and now I'm stuck.
The problem is that the entities of my JSON are not always the same. The JSON is something like:

"entries":[
{
"summary": "here is the sunnary",
"extensions": {
   "coordinates":"coords",
   "address":"address",
   "name":"name"
   "telephone":"123123"
   "url":"www.blablablah"
},
}
]

I can move through the JSON, for example:

for entrie in entries:
  name =entrie['extensions']['name']
  tel=entrie['extensions']['telephone']

The problem comes because sometimes, the JSON does not have all the "fields", for example, the telephone field, sometimes is missing, so, the script fails with KeyError, because the key telephone is missing in this entry.
So, my question: how could I run this script, leaving a blank space where telephone is missing? I've tried with:

if entrie['extensions']['telephone']:
    tel=entrie['extensions']['telephone']

but I think is not ok.

like image 448
Pablo Pardo Avatar asked May 10 '13 23:05

Pablo Pardo


People also ask

How do you create an empty JSON in Python?

How to create a JSON file in Python. To create a json file in Python, use with open() function. The open() function takes the file name and mode as an argument. If the file is not there, then it will be created.

What is JSON dumps () method?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.

Can you have none in JSON?

JSON has a NULL data type, but it has not a None data type.

How do you parse JSON in Python?

Parse JSON - Convert from JSON to Python If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.

How to parse data from JSON using Python?

Here are some ways to parse data from JSON using Python below: Python JSON to Dictionary: With the help of json.loads () function, we can parse JSON objects to dictionary. Dictionary after parsing: {‘Name’: ‘nightfury1’, ‘Languages’: [‘Python’, ‘C++’, ‘PHP’]}

How to open a JSON file in Python?

Exploring the JSON file: Python comes with a built-in package called json for encoding and decoding JSON data and we will use the json.load function to load the file. Even if Python has an in-built library, we still need to know how to find the data we need. In our JSON file, you will see two top level keys:

What does JSON look like in Python?

Here is an example of a JSON string: In this example, JSON data looks like a Python dictionary. Just like dictionaries, JSON contains data in key-value pairs. However, the JSON data can also be a string, a number, a boolean, or a list.

How to parse a JSON string that returns a dictionary?

JSON (JavaScript Object Notation) is a popular way to structure data and is used to exchange information between a web application and the server. If you need to parse a JSON string that returns a dictionary, then you can use the json.loads () method.


1 Answers

Use dict.get instead of []:

entries['extensions'].get('telephone', '')

Or, simply:

entries['extensions'].get('telephone')

get will return the second argument (default, None) instead of raising a KeyError when the key is not found.

like image 197
Stefano Sanfilippo Avatar answered Oct 07 '22 19:10

Stefano Sanfilippo