Hwende
Hwende

Reputation: 649

Parse multiple JSON objects

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 ,

Upvotes: 0

Views: 3308

Answers (3)

John Foley
John Foley

Reputation: 4479

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"}]'; 

Upvotes: 4

rundelis
rundelis

Reputation: 35

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);

Upvotes: -1

Andy
Andy

Reputation: 63550

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"}]'; 

Upvotes: 2

Related Questions