Skip to content
 
 

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Working with JSON Data

https://www.youtube.com/watch?v=9N6a-VLBa2I&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU


JSON to Python

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

Reference: https://docs.python.org/3/library/json.html#encoders-and-decoders

Parse JSON to Python

Parse Python to JSON

REST to Python to JSON

See here

Using json.loads

Parsing JSON data from a STRING

import json

people_string = '''
{
  "people": [
    {
      "name": "John Smith",
      "phone": "615-555-7164",
      "emails": ["johnsmith@bogusemail.com", "john.smith@work-place.com"],
      "has_license": false
    },
    {
      "name": "Jane Doe",
      "phone": "560-555-5153",
      "emails": null,
      "has_license": true
    }
  ]
}
'''

data = json.loads(people_string)

print(type(data)) # => <class 'dict'>
print(type(data['people'])) # => <class 'list'>
print(data) # => {'people': [{'name': 'John Smith', 'phone': '615-555-7164', 'emails': ['johnsmith@bogusemail.com', 'john.smith@work-place.com'], 'has_license': False}, {'name': 'Jane Doe', 'phone': '560-555-5153', 'emails': None, 'has_license': True}]}

for people in data['people']:
    print(people)

Using json.load

Parsing data from a FILE

data.json

{
  "people": [
    {
      "name": "John Smith",
      "phone": "615-555-7164",
      "emails": ["johnsmith@bogusemail.com", "john.smith@work-place.com"],
      "has_license": false
    },
    {
      "name": "Jane Doe",
      "phone": "560-555-5153",
      "emails": null,
      "has_license": true
    }
  ]
}
import json

# with open('data.json', 'r') as rf:
with open('data.json') as f: # Read-only by default, so 'r' is not required
    data = json.load(f)

print(type(data)) # => <class 'dict'>
print(type(data['people'])) # => <class 'list'>
print(data) # => {'people': [{'name': 'John Smith', 'phone': '615-555-7164', 'emails': ['johnsmith@bogusemail.com', 'john.smith@work-place.com'], 'has_license': False}, {'name': 'Jane Doe', 'phone': '560-555-5153', 'emails': None, 'has_license': True}]}

for people in data['people']:
    print(people)

Using json.dumps

import json

...

for people in data['people']:
    print(people)
    del people['phone']

new_string = json.dumps(data)

print(new_string) # => {"people": [{"name": "John Smith", "emails": ["johnsmith@bogusemail.com", "john.smith@work-place.com"], "has_license": false}, {"name": "Jane Doe", "emails": null, "has_license": true}]}

Make it more readable

indented_string = json.dumps(data, indent=2)
print(indented_string) # =>
# {
#   "people": [
#     {
#       "name": "John Smith",
#       "emails": [
#         "johnsmith@bogusemail.com",
#         "john.smith@work-place.com"
#       ],
#       "has_license": false
#     },
#     {
#       "name": "Jane Doe",
#       "emails": null,
#       "has_license": true
#     }
#   ]
# }

Using json.dump

import json
...
indented_string = json.dumps(data, indent=2)
print(indented_string)

# Creates a new JSON file
with open('data-copy.json', 'w') as f:
    json.dump(data, f, indent=2)

REST to JSON file

Using the Python built-in HTTP request

from urllib.request import urlopen

rest_api = 'https://jsonplaceholder.typicode.com/users'

try:
    with urlopen(rest_api) as response:
        source = response.read()
        source_json = json.loads(source)

    with open('rest-copy.json', 'w') as f:
        json.dump(source_json, f, indent=2)
except:
    print('Unable to fetch rest_api')

References

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

About

Learning Python programming language

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages