Q:

json load

import json

with open('data.txt') as json_file:
    data = json.load(json_file)
0

  import json

x = {
  "name": 
  "John",
  "age": 30,
  "married": True,
  
  "divorced": False,
  "children": ("Ann","Billy"),
  "pets": 
  None,
  "cars": [
    {"model": "BMW 230", "mpg": 
  27.5},
    {"model": "Ford Edge", "mpg": 24.1}
  ]

  }

print(json.dumps(x))
 
6
# Python program to read 
# json file 
   
   
import json 
   
# Opening JSON file 
f = open('data.json',) 
   
# returns JSON object as  
# a dictionary 
data = json.load(f) 
   
# Iterating through the json 
# list 
for i in data['emp_details']: 
    print(i) 
   
# Closing file 
f.close() 
2

import json

with open('path_to_file/person.json') as f:
  data = json.load(f)
1

  import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New 
  York"}'

# parse x:
y = json.loads(x)

# the result is a 
  Python dictionary:
print(y["age"]) 
3
# a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
y = json.dumps(x)
2

    json.dumps(x, indent=4, sort_keys=True)
 
2
>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('"\\"foo\\bar"')
'"foo\x08ar'
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)
['streaming API']
1

import json

with open('path_to_file/person.json') as f:
  data = json.load(f)

# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
print(data)
0

    json.dumps(x, indent=4, separators=(". ", " = "))
 
0

New to Communities?

Join the community