Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Json object in javascript

I am in process to validate a form where i need to show certain radio buttons and user need to select them based on some rules,how many number of radio buttons can be created is dynamic so i can not do validation on server side not can write a predefined java-script code for that.

each of the radio buttons will be divided in to groups say required and than further down they can be grouped like center,left, right etc, so from each group user need to select one value, so the structure comes out like this

-Main Group (if block needs to validate based on this e.g if key=required should validate)
  |
  Sub-group (say left, right etc)
   |
   number of radio buttons based on the sub-group

So the main group key can be used to decide if validation should be done on that or not and based on the sub-group key i can decide what all values will be there and needs to be validate

i was planning to create a JSON object on page rendering time like

{"required": [
        {"center": "id1,id2,id3"},
        {"left": "id1,id2,id3"}
      ]
  "optional": [
        {"center": "id1,id2,id3"},
        {"left": "id1,id2,id3"}
      ]
};

i am not sure if the structure i am thinking is right and how to create it in java script? like i have a external loop for key and than one more loop for the sub-group and finally for the buttons in the sub-group,

 for(main group key){
     for(subgroup key){
       for(list of radio button under subgroup key)
}
  }

but not sure how to create a right structure so that i can parse it later with jquery and use that for validation.

Any help in this will really be appreciated.

like image 918
Umesh Awasthi Avatar asked Sep 15 '12 19:09

Umesh Awasthi


People also ask

How do you create a JSON object?

To create an object we need to use opening and closing curly braces {} and then inside of that we'll put all of the key value pairs that make up our object. Every single property inside the JSON is a key value pair. The key must be surrounded by double "" quotes followed by a colon : and then the value for that key.

Can you create JSON file in JavaScript?

* The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.

Is JSON an object in JavaScript?

JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.

What is a JSON object example?

JSON Object Example A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.


1 Answers

In javascript. You can use JSON.stringify(myObject, replacer);

For example.

create on javascript object like this

var myObject={};

now after creating javascript object you can convert it into JSON structure like this

var myJsonText=JSON.stringify(myObject);

NOTE: replacer is optional

Now if you want to convert it in JSON Object Use JSON.parse method

myJsonObject=JSON.parse(myJsonText)
like image 152
Ashirvad Avatar answered Oct 31 '22 14:10

Ashirvad