2

Im trying to Push a valid json string to javascript json object, but every time im trying to do it like that:

markersData['values'] = [string];

the result is of markersData json object is:

"values":["{'latLng..."

instead of (Original):

"values":[{"latLng...

it take all of the json and push it as one variable (invalid json), how can i push it as a part of the original json?

any idea how to solve it?

Thank you!

1

4 Answers 4

3

You need to deserialise the JSON string before setting it to the property of the object:

markersData['values'] = [JSON.parse(yourJsonString)];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! thats was helpful!
0
markersData['values'] = [JSON.parse(string)];

Hope this helps.. Read more about JSON.parse here

Comments

0

You need to parse the string first.

JSON.parse(addstringvar);

Code pen demo

var testObj = {};
var addString = '{"name": "test"}';
testObj.values = [JSON.parse(addString)];

Comments

0

You'll need to make sure you have a valid JSON. So below will show you how to create an easy JSON which will be valid for you to use

JSON Object:

var newObject = {};
newObject.Latlng = "ValueHere";

var jsonString = JSON.stringify(newObject);

// Check jsonString before you parse for pushing.
console.log(jsonString);

You will need to deserialise the JSON string before setting it to the property of the object

like Rory McCrossan mentions in his answer

jsonString[value] = [JSON.parse(jsonString)];

2 Comments

The question isn't entirely clear, but I think the OP wants the reverse of this.
@RoryMcCrossan Cheers, I've quoted you in my answer. His question is 100% clear, but he needs advise on how to make a valid json before he can achieve anything.

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.