Sabrina
0
Q:

parse json python

import json

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

print(data)
28
import json

x =  '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)

print(y["age"]) 
8
import json

json = """{
    "name":"John",
    "age":30,
    "city":"New York"
}"""

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

  import json

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

# parse x:
y = json.loads(x)
1
# a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

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

x = {
  "name": 
  "John",
  "age": 30,
  "city": "New York"
}

y = json.dumps(x)
0
my_string = '[{"name": "sarah", "grade": 10}]'  # create json
my_json_string = json.loads(my_string)          # turn it in a dictionary
print(my_json_string[0]['name'])                # [0] in list we have to assess to the related element
0

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

New to Communities?

Join the community