1

Iam trying to edit my json file using javascript. My json file code is

{
"contacts": {
    "contact": {
        "info": [
            {
                "id": "0",
                "name": "name",
                "number": "0",
                "email": "email"
            }
        ],
        "messages": [
            {
                "message": [
                    {
                        "-from": "0",
                        "-to": "0",
                        "info": {
                            "id": "0",
                            "name": "name",
                            "number": "0",
                            "email": "email"
                        },
                        "text": [
                            {
                                "subText": {
                                    "-italics": "false",
                                    "-underline": "false"
                                }
                            }
                        ],
                        "file": [
                            {
                                "-size": "0",
                                "-type": "image",
                                "fileInfo": {
                                    "id": "0",
                                    "name": "name",
                                    "number": "0",
                                    "email": "email"
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
}

I want to set the value of contacts.contact.info.id and i want to put another array in contacts.contact.info. plz help me with the syntax . iam facing problem while creating while creating new element in info array. and tell how to display values of the contacts.contact.info element

1
  • One thing the answerers haven't mentioned is that you need to ensure that you use JSON.parse(json) to get your data in a usable format before you update it. Commented Jun 18, 2014 at 9:37

3 Answers 3

2

Using JavaScript, it's perfectly simple:

var data = {};//your json string here
data.contacts.contact.info[0].id = 'New value here'; //id is in array
data.contacts.contact.info.push('Append this value to info');
//or
data.contacts.contact.info = 'Overwrite current info value';
//get string
console.log(JSON.stringify(data));

That's all. If you want to manually alter the data:

{
"contacts": {
    "contact": {
        "info": [
            {
                "id": "0",//replace with, for example:
                "name": "name",
                "number": "0",
                "email": "email"
            },//add comma, and data below:
            {
                "id": "123",
                "name": "foobar",
                "number": "1"
            },
            "primitives go here, too"
        ],//end array
        "messages": [...

That's all there is to it.
To preserve the pretty-printing, as you requested:

var data = JSON.parse(yourString);
data.contacts.contact.info.push(
    {"id": "7","name": "dg","number": "5454","email": "fsfsf"}
);
data.contacts.contact.info.push(
    {"id": "8","name": "gjhhk","number": "64564","email": "fs54f"}
);
//get pretty string, using spaces for indentation:
console.log(JSON.stringify(data, null, ' '));
//same thing, but using tabs:
console.log(JSON.stringify(data, null, '\t'));
Sign up to request clarification or add additional context in comments.

10 Comments

i have stored my json data in file and im fetching it and parsing it using json.parse(); and inserting data using data.contacts.contact.info = 'Overwrite current info value'; but when i use data.contacts.contact.info.push('Append this value to info'); , its not working.
@Leftalone: if you do that, then data.contacts.contact.info won't be an array anymore. If you want to add data, use the push method on the array. To replace, consider assigning a new array, to ensure data structure consistency
if i have a array inside a array, how to fetch it.
@Leftalone: that's pretty basic: suppose var matrix = [[1,2],[3,4]]; just use 2 indexes: console.log((matrix[0][0] + matrix[0][1]) === matrix[1][0]); will log true, because 1 + 2 === 3
the json.contacts.contact.info.push({data}; method is working only once. if i have two elements in the info array the push thing is not working.
|
1

You may try this:

var json={contacts:{contact:{info:[{id:"0",name:"name",number:"0",email:"email"}],messages:[{message:[{"-from":"0","-to":"0",info:{id:"0",name:"name",number:"0",email:"email"},text:[{subText:{"-italics":"false","-underline":"false"}}],file:[{"-size":"0","-type":"image",fileInfo:{id:"0",name:"name",number:"0",email:"email"}}]}]}]}}};

console.log(json.contacts.contact.info[0]); //access first record

json.contacts.contact.info.push({id:"1",name:"name",number:"1",email:"[email protected]"}); // add new info

console.log(json.contacts.contact.info);

3 Comments

hi arvind what to do in case of array in a array, if i have to update contacts.messages.message.info.id .
@Leftalone you may use json.contacts.contact.messages[0].message[0].info.id='new value'
the json.contacts.contact.info.push({data}; method is working only once. if i have two elements in the info array the push thing is not working.
0

for contacts.contact.info.id :

contacts.contact.info is an array.. so you have to work by index like:

contacts.contact.info[0].id = newID;

to Add to this array:

contacts.contact.info.push(newArrayItem);

hope this answers your questions..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.