Home > OS >  Parse a json that contain json string
Parse a json that contain json string

Time:09-17

I have a json which has another json inside it. But it is inside double quotes due to this it is given me a parsing error. Is there any way to parse this json other than using gsub to replace the double quote..

obj={Name:"{\"FirstName\":\"Douglas\",\"LastName\":\"Crockford\"}"}

I want it like this

{Name:{FirstName:"Douglas",LastName:"Crockford"}}

Is there any way to achieve this using Ruby?

CodePudding user response:

Just call JSON.parse on the values again:

obj.transform_values { |v| JSON.parse(v) }
#=> {:Name=>{"FirstName"=>"Douglas", "LastName"=>"Crockford"}}

When you are still on Ruby <2.4 then there a more steps:

obj.map { |k, v| [k, JSON.parse(v)] }.to_h 
  • Related