Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add key and a value to a json using javascript or jquery

Tags:

i have a json variable like this

var jsondata={"all":"true"} 

i want to push another key and value to the jsondata. after that my jsondata have to be like this.

{"all":"true","FDamount":"false","DDamount":"true"} 

how to do that??? i tried jsondata.push({"FDamount":"false"}) and jsondata.push("FDamount:false"). both of these method is not working.

thank you

like image 650
JSAddict Avatar asked Dec 22 '12 11:12

JSAddict


People also ask

How do I add a key-value pair to a JSON?

To add a new key value pair in existing JSON object using JavaScript, we can parse the JSON string with JSON. parse , then add the key-value pairs we want, and then convert the object back to a JSON string with JSON. stringify . to parse s into an object with JSON.

How do you add a key-value pair to a JSON object in Python?

import json with open("your_json_file. txt", 'r') as f: data = json. loads(f. read()) #data becomes a dictionary #do things with data here data['ADDED_KEY'] = 'ADDED_VALUE' #and then just write the data back on the file with open("your_json_file.

What is key and value in JSON?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).

Can an object be a key in JSON?

JSON objects are written in key/value pairs. JSON objects are surrounded by curly braces { } . Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.


1 Answers

Like this

jsondata.FDamount = 'false'; // or jsondata['FDamount'] = 'false'; 
like image 82
senK Avatar answered Oct 26 '22 19:10

senK