1

I have a variable name state, how can i convert it into JSON object and changes its values and then convert it back to string.

Note: dataField value itself is another JSON, thats where i am having problem.

state='{"instanceId":"iaFMRu32kedGmOHC8umMQ","**dataField**":"{"id":52,"name":"CropSelection"}","validation":"incomplete","required":true,"value":"","hasValue":false,"requiresSave":true}';
1
  • 1
    Is state a string or is it an object? Commented Aug 22, 2018 at 18:22

5 Answers 5

3

You can convert it with JSON.parse()

var obj = JSON.parse(state);

then you modify what you want:

obj.dataField.name = "newName";

and finally you get your string back to the state:

state = JSON.stringify(obj);
Sign up to request clarification or add additional context in comments.

2 Comments

i already tried that, it does'nt work for because there is another value in JSON format. e.g "dataField":"{"id":52,"name":"CropSelection"}".
@atul1039 See my answer. It resolves your malformed JSON
0

JSON.parse() can be used for converting string into JSON

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

JSON.stringify() can be used for converting JSON back to string

var myJSON = JSON.stringify(obj);

Comments

0

You can do JSON.parse to convert the string to json & then update the value of dataField and again do JSON.stringify

Note in the string "dataField":"{"id":52,"name":"CropSelection"}" this is wrong, It need to be "dataField":{"id":52,"name":"CropSelection"}.

let newDT = {
  "id": 100,
  "name": "test"
}

let state = '{"instanceId":"iaFMRu32kedGmOHC8umMQ","dataField":{"id":52,"name":"CropSelection"},"validation":"incomplete","required":true,"value":"","hasValue":false,"requiresSave":true}';

let k = JSON.parse(state);
k.dataField = newDT
console.log(JSON.stringify(k))

Comments

0

You can use the build in JSON functions. I also fixed your JSON string. You can surrounded your "dataField" value with quotes which it doesn't need. It should be {"id":52, "name":"CropSelection"} instead of "{"id":52, "name":"CropSelection"}".

var state = '{"instanceId":"iaFMRu32kedGmOHC8umMQ","dataField":{"id":52, "name":"CropSelection"},"validation":"incomplete","required":true,"value":"","hasValue":false,"requiresSave":true}';

var obj = JSON.parse(state);

obj.dataField.name = 'newName';

console.log(JSON.stringify(obj));

Comments

0

Posting this answer in case you posted your json incorrectly, and the value of the dataField is indeed a nested string containing json. If so, you can parse the state, and then parse the dataField. Change the parsed data field, then convert it back to strings incrementally.

var state = '{"instanceId":"iaFMRu32kedGmOHC8umMQ","dataField":"{\\"id\\":52,\\"name\\":\\"CropSelection\\"}","validation":"incomplete","required":true,"value":"","hasValue":false,"requiresSave":true}';

var parsedState = JSON.parse(state);
console.log( parsedState );
var parsedDataField = JSON.parse( parsedState.dataField );
console.log( JSON.parse( parsedState.dataField ) );

parsedDataField.name = "New Name";
parsedState.dataField = JSON.stringify( parsedDataField );
state = JSON.stringify( parsedState );
console.log( state );

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.