3

I have some web services that receive JSON data send by jquery method. But I need to edit the object before send this data. Is there any way to parse a JSON object to a simple object in javascript, modify it and then parse it again to JSON. or maybe update this JSON object without parse it?

1
  • I realized that is a string, I had to use JSON.Parse to get the JSON object Commented Jan 5, 2012 at 21:40

5 Answers 5

6

To go from a JSON string to a JavaScript object: JSON.parse, or $.parseJSON if you're using jQuery and concerned about compatibility with older browsers.

To go from a JavaScript object to a JSON string: JSON.stringify.


If I've already do this var myData = JSON.stringify({ oJson:{data1 :1}}); and then I want to update that information setting data1 = 2, what is the best way to do this?

var myData = JSON.stringify({ oJson:{data1 :1}});
// later...
parsedData = JSON.parse(myData);
parsedData.oJson.data1 = 2;
myData = JSON.stringify(parsedData);

Even better though, if you save a reference to the object before stringifying it, you don't have to parse the JSON at all:

var obj = { oJson:{data1 :1}};
var myData = JSON.stringify(obj);
// later...
obj.oJson.data1 = 2;
myData = JSON.stringify(obj);
Sign up to request clarification or add additional context in comments.

4 Comments

If I've already do this var myData = JSON.stringify({ oJson:{data1 :1}}); and then I want to update that information setting data1 = 2, what is the best way to do this?
@Juan: Why don't you manipulate the data before stringify it? All you need to know is in the answer anyway. What more do you need?
How about just setting data1 to 2 before stringifying it?
Because the last value depends on an ajax response during the execution of a process
3
var parsed = JSON.parse('{"a": 1}');
parsed.b = 2;
var string = JSON.stringify(parsed);
//string is: '{"a":1,"b":2}'

Comments

0

I think something like the following should work...

//Convert your JSON object into a JavaScript object
var myObject = JSON.parse(json);

//You can then manipulate the JavaScript object like any other
myObject.SomeValue = "new";

//Then you can convert back to a JSON formatted string
json = JSON.stringify(myObject);

Comments

0

As JSON is an JavaScript object you can simply manipulate it with JavaScript.

4 Comments

No, JSON is a data exchange format and only exist as string in JavaScript. But true, JavaScript provides string methods as well.
But also a JavaScript object in the context of JavaScript, maybe you mean a JSON string.
Maybe you refer to object literals as JSON as well, but that does not make it correct. JSON is the official name of the data-interchange format, so if you talk about JSON I have to assume you are talking about this format. In fact, the only point where the specification talks about JSON is when it describes the JSON object with its parse and stringify methods. Otherwise, object literals are also called object initializers.
And he's rigth that a simple eval() can be used to parse any valid JSON object, however it's discouraged as it contains security holes.
0

You could do something like this to get a javascript object:

var jsObject = JSON.parse(jsonString);

Then you could modify jsObject and turn it back into a JSON string with JSON.stringify.

This page has more information on it.

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.