0

I'm able to parse a single object using JSON.parse.

var testing = '{"appid": "730", "contextid": "2", "amount": "1", "assetid": "2883267603"}';
var itemsObject = JSON.parse(testing);

But when I try to parse a variable with multiple objects:

var testing = '{"appid": "730", "contextid": "2", "amount": "1", "assetid": "2883267603"}, {"appid": "730", "contextid": "2", "amount": "1", "assetid": "3084880561"}'; 
var itemsObject = JSON.parse(testing);

I get the following error:

SyntaxError: Unexpected token ,
1
  • 1
    you try to parse an Array, try put [ and ] at the begining and the closing of the string Commented Aug 13, 2015 at 12:43

3 Answers 3

4

You need to make an array of objects

var testing = '[{"appid": "730", "contextid": "2", "amount": "1", "assetid": "2883267603"}, {"appid": "730", "contextid": "2", "amount": "1", "assetid": "3084880561"}]'; 
Sign up to request clarification or add additional context in comments.

Comments

2

Because testing is now an array of (as you say - multiple) objects, and you should add square brackets around them to indicate that:

var testing = '[{"appid": "730", "contextid": "2", "amount": "1", "assetid": "2883267603"}, {"appid": "730", "contextid": "2", "amount": "1", "assetid": "3084880561"}]'; 

Comments

-1

Try this : var testing = '[{"appid": "730", "contextid": "2", "amount": "1", "assetid": "2883267603"}, {"appid": "730", "contextid": "2", "amount": "1", "assetid": "3084880561"}]'; var itemsObject = JSON.parse(testing);

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.