JSON, short for JavaScript Object Notation, is an open standard. Although its name doesn't imply so, it is a language-independent data format. JSON is used to both store and exchange data. It's easy to read and write for humans too. Working with JSON in python is very easy! Python has two data types that, together, form the perfect tool for working with JSON in Python: dictionaries and lists.
Importing the JSON library in Python
Python ships with a powerful and elegant JSON library to help you decode and encode JSON.
How to parse JSON in Python
Parsing a string of JSON data or JSON file is called decoding JSON.
json.loads (...) loads is short for load string
json.load (...) load a json file
It converts:
- objects to dictionaries
- array to lists
- boolean, integers, float, and strings are converted into the correct types in python
- Any null will be converted into python's None type
output
{'articles': [{'title': 'mutable vs immutable', 'author': 'nazeeruddin', 'emails': ['leanerlandmark@gmail.com', 'testmail@gmail.com'],
'phone': '9693996999', 'is_approved': True}, {'title': 'list vs tuple', 'author': 'satish', 'emails': None, 'phone': '9849084100', 'is_approved': False}]}
<class 'dict'>
<class 'list'>
<class 'str'>
<class 'list'>
<class 'bool'>
Here, entire json string is loaded as a dictionary object, key is articles and list is a value.
list is a collection of dictionary objects, so we can iterate through each object and can access every attribute as a key.
following code print's each 'aritcle' object
following code print's title of each article object
following code remove's attribute 'phone' from each 'article' object, and dumps to a new string.
dump the dictionary 'data' object to a new string:
How to read a JSON file in python
Besides json.loads (), there's also a function called json.load (), it will load data from a file.
following mock data was generated using a website url: https://mockaroo.com/
following code snippet loads data from a file:
How to write JSON to a file in python
Working with Yahoo Finance API
The financial data from all methods is returned as JSON.
You can run multiple symbols at once using an inputted array or run an individual symbol using an inputted string.
If yahoofinancials is not present in your dev environment, run the below code to install it.
pip install yahoofinancials
OUTPUT
{
"EURUSD=X": {
"eventsData": {},
"firstTradeDate": {
"formatted_date": "2003-12-01",
"date": 1070236800
},
"currency": "USD",
"instrumentType": "CURRENCY",
"timeZone": {
"gmtOffset": 0
},
"prices": [
{
"date": 1674604800,
"high": 1.0922634601593018,
"low": 1.0858116149902344,
"open": 1.088814616203308,
"close": 1.088814616203308,
"volume": 0,
"adjclose": 1.088814616203308,
"formatted_date": "2023-01-25"
},
{
"date": 1674691200,
"high": 1.0932546854019165,
"low": 1.0851635932922363,
"open": 1.0922038555145264,
"close": 1.0922038555145264,
"volume": 0,
"adjclose": 1.0922038555145264,
"formatted_date": "2023-01-26"
},
...
following code print each currency with date, and closing price:
OUTPUT
currency: EURUSD=X
date:2023-01-25 - closing_price:1.088814616203308
date:2023-01-26 - closing_price:1.0922038555145264
date:2023-01-27 - closing_price:1.0894432067871094
date:2023-01-30 - closing_price:1.0871455669403076
date:2023-01-31 - closing_price:1.085894227027893
currency: JPY=X
date:2023-01-25 - closing_price:130.23899841308594
date:2023-01-26 - closing_price:129.23699951171875
date:2023-01-27 - closing_price:129.8000030517578
date:2023-01-30 - closing_price:129.85400390625
date:2023-01-31 - closing_price:129.9499969482422
currency: GBPUSD=X
date:2023-01-25 - closing_price:1.232923984527588
date:2023-01-26 - closing_price:1.2412492036819458
date:2023-01-27 - closing_price:1.2415266036987305
date:2023-01-30 - closing_price:1.2400301694869995
date:2023-01-31 - closing_price:1.2318304777145386
Comments