Question
· Feb 12, 2020

Convert String to JSON

Hello,

 

We would like to convert a string which represents a dynamic JSON object, to a JSON.

 

We have:


linea: {"app_id":"5cf57b56-c3b4-4a0d-8938-4ac4466f93af","headings":{"en":"Cita Atención Primaria","es":"Cita Atención Primaria"},"subtitle":{"en":"C.P. ISORA","es":"C.P. ISORA"},"contents":{"en":"Aqui el contenido del mensaje si aplicase","es":"Aqui el contenido del mensaje si aplicase"},"data":"{\"centro\":\"C.P. ISORA\",\"fecha\":\"yyy/mm/dd\",\"hora\":\"hh:mm\",\"profesional\":\"nombre del profesional\",\"nomUsuario\":\"nombre de usuario\",\"codcita\":\"idCita\",\"sepuedeborrar\":\"1\"}","include_player_ids":["c2917a6f-6ecf-4f45-8b31-9b72538580fd"]}

 

We observe that data's value is enclosed in "", and quotes are being escaped with \.

 

We would need to convert it to JSON, to be able to send it to an external API, because it is sending to us:

 

{
    "informacion": "{\"errors\":[\"Data must be a valid JSON object and cannot be a string, a number, true, or false. See bit.ly/2iBky3H for an example of a JSON object. You can use jsonlint.com as a JSON validator.\"]}",
    "error": null
}

 

We have read:

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

https://community.intersystems.com/post/convert-json-string

 

How could we convert a string to JSON?
 

Discussion (2)2
Log in or sign up to continue

The source for this JSON seems to think that "data" holds a string rather than an object. Still, we can convert that back to a proper object using %DynamicObject's %FromJSON() method:

USER>set myJSONObj={"app_id":"5cf57b56-c3b4-4a0d-8938-4ac4466f93af","headings":{"en":"Cita Atención Primaria","es":"Cita Atención Primaria"},"subtitle":{"en":"C.P. ISORA","es":"C.P. ISORA"},"contents":{"en":"Aqui el contenido del mensaje si aplicase","es":"Aqui el contenido del mensaje si aplicase"},"data":"{\"centro\":\"C.P. ISORA\",\"fecha\":\"yyy/mm/dd\",\"hora\":\"hh:mm\",\"profesional\":\"nombre del profesional\",\"nomUsuario\":\"nombre de usuario\",\"codcita\":\"idCita\",\"sepuedeborrar\":\"1\"}","include_player_ids":["c2917a6f-6ecf-4f45-8b31-9b72538580fd"]}
 
USER>write myJSONObj.data
{"centro":"C.P. ISORA","fecha":"yyy/mm/dd","hora":"hh:mm","profesional":"nombre del profesional","nomUsuario":"nombre de usuario","codcita":"idCita","sepuedeborrar":"1"}

USER>set dataObj=##class(%DynamicObject).%FromJSON(myJSONObj.data)
 
USER>write dataObj
11@%Library.DynamicObject

USER>set myJSONObj.data = dataObj
 
USER>write myJSONObj.%ToJSON()
{"app_id":"5cf57b56-c3b4-4a0d-8938-4ac4466f93af","headings":{"en":"Cita Atención Primaria","es":"Cita Atención Primaria"},"subtitle":{"en":"C.P. ISORA","es":"C.P. ISORA"},"contents":{"en":"Aqui el contenido del mensaje si aplicase","es":"Aqui el contenido del mensaje si aplicase"},"data":{"centro":"C.P. ISORA","fecha":"yyy/mm/dd","hora":"hh:mm","profesional":"nombre del profesional","nomUsuario":"nombre de usuario","codcita":"idCita","sepuedeborrar":"1"},"include_player_ids":["c2917a6f-6ecf-4f45-8b31-9b72538580fd"]}