Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create JSON object using jQuery

Tags:

json

arrays

I have a JSON object in below format:

temp:[         {            test:'test 1',            testData:  [                         {testName: 'do',testId:''}                          ],            testRcd:'value'                                      },         {             test:'test 2',            testData:  [                             {testName: 'do1',testId:''}                          ],            testRcd:'value'                                    }       ], 

How can i create JSON object in jquery for above format. I want to create a dynamic JSON object.

like image 898
user1462958 Avatar asked Jun 18 '12 07:06

user1462958


People also ask

How do I create a JSON file?

Open a Text editor like Notepad, Visual Studio Code, Sublime, or your favorite one. Copy and Paste below JSON data in Text Editor or create your own base on the What is JSON article. Once file data are validated, save the file with the extension of . json, and now you know how to create the Valid JSON document.

How do I create a JSON object dynamically in node JS?

To create JSON object dynamically via JavaScript, we can create the object we want. Then we call JSON. stringify to convert the object into a JSON string. let sitePersonnel = {}; let employees = []; sitePersonnel.

How do I create a JSON object in typescript?

obj: any; //new object declaration this. obj = { "col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"}, "col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, "col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} } this. output.

How does jQuery handle JSON data?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.


2 Answers

Just put your data into an Object like this:

var myObject = new Object(); myObject.name = "John"; myObject.age = 12; myObject.pets = ["cat", "dog"]; 

Afterwards stringify it via:

var myString = JSON.stringify(myObject); 

You don't need jQuery for this. It's pure JS.

like image 93
Tim Avatar answered Nov 12 '22 06:11

Tim


A "JSON object" doesn't make sense : JSON is an exchange format based on the structure of Javascript object declaration.

If you want to convert your javascript object to a json string, use JSON.stringify(yourObject);

If you want to create a javascript object, simply do it like this :

var yourObject = {           test:'test 1',           testData: [                  {testName: 'do',testId:''}           ],           testRcd:'value'    }; 
like image 22
Denys Séguret Avatar answered Nov 12 '22 07:11

Denys Séguret