How to format jbuilder value

I got a jbuilder response like below

{
  "name": "amb_devcernerpowerchart_com:patient_val2",
  "value": "{\"value\"=>\"patient_value\", \"expiration\"=>31536000, \"created_datetime\"=>\"2019-12-09T12:09:59Z\"}"
}

I want to only value part of value i.e i want output like below.

{
  "name": "amb_devcernerpowerchart_com:patient_val2",
  "value": "value"=>"patient_value"
}

How do i get this?

Jbuilder file looks like this

json.name @component.preference_data.id
json.value @component.preference_data.value

1 answer

  • answered 2019-12-09 13:26 Anuj Khandelwal

    From the question, it seems as though the data that you are receiving in the value field is a stringified JSON. You would have to parse the JSON and retrieve the required value as follows:

    json.value JSON.parse(@component.preference_data.value)['value']
    

    Basically, @component.preference_data.value returns a string, which is parsed into a JSON using JSON.parse. At the end of that, we get a hash whose 'value' field is retrieved.