soldiershin
soldiershin

Reputation: 1620

Update JSON.parsed object in javascript

I am trying to update the parsed object.data is the JSON object received from the server.

var document_text = JSON.parse(data.projectData[0]['document_text']);
for(var j = 0; j < document_text.length;j++)
{
    var k = JSON.parse(document_text[j]);
    var document_data = JSON.parse(k).text;
    var page_number = JSON.parse(k).page_number;
}

Now i want to update the document_text object which contains the text and the page_number field.Note that i have to parse the object two times.First to parse the outer value then to get the inner value.How can i update the the fields of document_text(i.e text,page_number).

This is the original data

 ["\"{\\\"text\\\":\\\"Osddsdsdsdsds \\\\n\\\\n to  as \\\\\\\"sdfdsdsfsdfsdfsdf\\\\\\\") and CPGsddsdsdsdssdROsdsdsdsdP sdsdds,  a \\\\sd  sdds\\\\n\\\\n\\\\f\\\",\\\"page_number\\\":44}\"","\"{\\\"text\\\":\\\"Page  14 \\\\n\\\\nsdfsdfsdfdscopysdsdds\\\n\\\\n\\\\f\\\",\\\"page_number\\\":45}]

Upvotes: 0

Views: 556

Answers (1)

Omar Himada
Omar Himada

Reputation: 2588

var document_text = JSON.parse(data.projectData[0]['document_text']);

/* At this point, document_text is already a JSON object. Iterating over it with 
a for loop doesn't make much sense. You can now just access its properties directly. */

document_text.text = "Some other text";
document_text.page_numer = 1;

/* Now we can return it to where it came from by stringify'ing it */

data.projectData[0]['document_text'] = JSON.stringify(document_text);

Upvotes: 1

Related Questions