Answers for "create a json in python"

13

python json string to object

import json

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

print(y["age"])
Posted by: Guest on May-14-2020
4

how to create json file in python

#import the json module
import json 

#create a dictionary which we can add to a json file
dictionary ={ 
    "name" : "sathiyajith", 
    "rollno" : 56, 
    "cgpa" : 8.6, 
    "phonenumber" : "9976770500"
} 

#open an object with following inputs: 'name_of_file.json', 'w'
#dump the content fromt he dictionary into the outfile
with open("sample.json", "w") as outfile: 
    json.dump(dictionary, outfile)
Posted by: Guest on November-20-2020
1

generate json in python

# JSON objects and JSON string objects can be created by reading a file directly 
# with the json.load() and json.dump() functions.

data_set = {"key1": [1, 2, 3], "key2": [4, 5, 6]}

json_dump = json.dumps(data_set)

print(json_dump)

# String of JSON object

OUTPUT
{"key1": [1, 2, 3], "key2": [4, 5, 6]}

json_object = json.loads(json_dump)

print(json_object["key1"])

# JSON object

OUTPUT
[1, 2, 3]
Posted by: Guest on July-05-2021
3

python to json

# a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
y = json.dumps(x)
Posted by: Guest on May-02-2020

Python Answers by Framework

Browse Popular Code Answers by Language