Question
Rick Prichett · Jan 28, 2021

Looking for a way to Parse or access a JSON response by tag

I have a business process that I call a class that returns a JSON message.  I was hoping to be able to reference the tag or iterate through the response until I find my value.

// Build the request parameter
set tRequest = ##class(CDSM.MemberAPI.Request).%New()
set tRequest.DateOfBirth = ptDOB 
set tRequest.FirstName = ptFname
set tRequest.LastName = ptLname // Business operation needs to be parameterized
set tSC = ..SendRequestSync("boEISLMemberAPI, tRequest")

Just pasting the value I am interested since the response is 300 values long.

<memberId>12345678</memberId>

 

Product version:
IRIS 2020.2
10
1 0 9 99

Replies

In Cache product line, you would need to use Dynamic Objects. In order to iterate through a JSON Object, you would need to know its structure. Most likely, it is a nested object so you would have drill down to it.

To read your JSON Object into a DynamicObject, use

Set tDynObject = {}.%FromJSON(yourJSONString).

Here is some reference for Dynamic Objects:

https://docs.intersystems.com/latest/csp/docbook/Doc.View.cls?KEY=GJSON

In IRIS Product Line, in addition to Dynamic Objects, you have JSON Adaptors. If your class inherits from %JSON.Adaptor, it can recognize JSON  key/value pairs and you can just assign them to the Properties in your object.

Here is a reference for JSON Adaptors:

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

This is very thorough.  Well done!

This is very thorough.  Well done!

Thank you. It's very useful

You can convert to a JSON string through the built-in method of the object

You can convert to a JSON string through the built-in method of the object

I'm not sure what you are trying to accomplish, JSON doesn't have tags and the example you provide is XML