Python JSON Data

Python JSON Data


JSON (short for JavaScript Object Notation) has become one of the quality formats for sending data by HTTP request between web browsers and other applications. it’s a way more free-form format than a tabular text form like CSV. Here is an example:

obj = """
{"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38,
"pets": ["Sixes", "Stache", "Cisco"]}]


JSON may be a very nearly valid Python code with the exception of its null value null and a few other nuances (such as disallowing trailing commas at the top of lists). the essential types are objects (dicts), arrays (lists), strings, numbers, booleans, and nulls. All of the keys in an object must be strings. There are several Python libraries for reading and writing JSON data. I’ll use JSON here because it is made into the Python standard library. To convert a JSON string to Python form, use json.loads :
In [62]: import json
In [63]: result = json.loads(obj)
In [64]: result
{'name': 'Wes',
'pet': None,
'places_lived': ['United States', 'Spain', 'Germany'],
'siblings': [{'age': 30, 'name': 'Scott', 'pets': ['Zeus', 'Zuko']},
{'age': 38, 'name': 'Katie', 'pets': ['Sixes', 'Stache', 'Cisco']}]}
json.dumps , on the opposite hand, converts a Python object back to JSON: In [65]: asjson = json.dumps(result)
How we convert a JSON object or list of objects to a DataFrame or another arrangement for analysis is going to be up to you. Conveniently, you’ll pass an inventory of dicts (which were previously JSON objects) to the DataFrame constructor and choose a sub-set of the info fields:
In [66]: siblings = pd.DataFrame(result['siblings'], columns=['name', 'age'])
In [67]: siblings
name age
0 Scott  30
1 Katie  38

The pandas.read_json can automatically convert JSON datasets in specific arrange‐ ments into a Series or DataFrame. For example:

In [68]: !cat examples/example.json
[{"a": 1, "b": 2, "c": 3},
{"a": 4, "b": 5, "c": 6},
{"a": 7, "b": 8, "c": 9}]

The default options for pandas.read_json assume that every object within the JSON array is a row within the table:

In [69]: data = pd.read_json('examples/example.json')
In [70]:
   a b c
0 1 2 3
1 4 5 6
2 7 8 9

For an extended example of reading and manipulating JSON data (including nested records). If we’d like to export data from pandas to JSONa method is to use the to_json methods on Series and DataFrame:

In [71]: print(data.to_json())
In [72]: print(data.to_json(orient='records'))
Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 421

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top