user502052
user502052

Reputation: 15258

Trouble on parsing JSON data

What is wrong in parsing following JSON data

'{
   {"errors":
     {"firstname":"is too short"}
   },
   {"account":
     {"firstname":"Test"}
   }
}'

for which a get this error?

JSON::ParserError in AccountsController#home
706: unexpected token at ... # the code above

?

In the AccountsController I have

JSON.parse(json_data)["errors"]

Upvotes: 1

Views: 1226

Answers (3)

DanneManne
DanneManne

Reputation: 21180

You should not encapsulate the attribute errors and account. It should probably look like this:

'{
  "errors":{"firstname":"is too short"},
  "account":{"firstname":"Test"}
}'

Upvotes: 4

awm
awm

Reputation: 6580

You're missing property names:

'{"property1":
   {"errors":
     {"firstname":"is too short"}
   },
  "property2":
   {"account":
     {"firstname":"Test"}
   }
}'

Or, you really wanted an array:

'[
   {"errors":
     {"firstname":"is too short"}
   },
   {"account":
     {"firstname":"Test"}
   }
]'

Upvotes: 3

Satya
Satya

Reputation: 4478

Your data struct appears to be a JSON object {} but you have an array []. The first set of {} should be [].

http://www.jsonlint.com/

Upvotes: 1

Related Questions