from this
["{\r\n \"charge_type\": \"sports\",\r\n \"amount\": 1000\r\n},{\r\n \"charge_type\": \"servicing\",\r\n \"amount\": 550\r\n}"]
to this
[{"charge_type"=>"sports", "amount"=> "1000"},{"charge_type"=>"servicing", "amount"=> "500"}]
I have tried this,
JSON.parse(["{\r\n \"charge_type\": \"sports\",\r\n \"amount\": 1000\r\n},{\r\n \"charge_type\": \"servicing\",\r\n \"amount\": 550\r\n}"].first)
But could not figure it out. Any help will be appreciated.
CodePudding user response:
It's not clear what you're trying to do with the [...].first
part. What you really need is:
JSON.parse("[{\r\n \"charge_type\": \"sports\",\r\n \"amount\": 1000\r\n},{\r\n \"charge_type\": \"servicing\",\r\n \"amount\": 550\r\n}]")
CodePudding user response:
You are having problem because your array []
is not wrapped around string, it need to be "[]"
then it will work.
e.g your current json will give error
unexpected token at ',{
"charge_type": "servicing",
"amount": 550
but if your json is completely wrapped inside string like this (notice "
around array)
my_json = "[{\r\n \"charge_type\": \"sports\",\r\n \"amount\": 1000\r\n},{\r\n \"charge_type\": \"servicing\",\r\n \"amount\": 550\r\n}]"
then you can do something like this
JSON.parse(my_json)
which will return
[{"charge_type"=>"sports", "amount"=>1000}, {"charge_type"=>"servicing", "amount"=>550}]