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.
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.
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.
JSON has a NULL data type, but it has not a None data type.
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.
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’]}
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With