Answers for "how to json"

9

change js to json

var obj = {name: "Martin", age: 30, country: "United States"};
 
// Converting JS object to JSON string
var json = JSON.stringify(obj);
 
console.log(json);
// Prints: {"name":"Martin","age":30,"country":"United States"} 
"https://www.tutorialrepublic.com/faq/how-to-convert-js-object-to-json-string.php"
Posted by: Guest on May-16-2020
41

Javascript object to JSON string

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
Posted by: Guest on July-23-2019
0

how to use json

#first save a JSON file in the same directory
#in the json type {} inside also

import JSON

#getting data from the json
def get_data():
	with open("json_name.json", "r") as f:
		data = json.load(f)
	return data

#saving data
def save_data(data):
	with open("json_name.json", "w") as f:
		json.dump(data, f)
#this is how we get data in json
data = get_data()
#this is how we change values in the json
data['test'] = {'name': 'Siamese'}
#this is how we save the changes in the json
save_data(data)

data = get_data()
print(data['test']['name'])
#output will be "Siamese"
Posted by: Guest on June-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language