Written by

Sr Application Development Analyst at The Ohio State University Wexner Medical Center
Question Scott Roth · May 28

JSON - Field extraction using Object Script

I am struggling on how to pull out individual fields from a JSON. In a previous post someone had mentioned that I could...

If ((tHttpResponseIsObject) && ($ZCONVERT(tHttpResponseContentType,"L") [ "application/json"))
  {
    set responseData = {}.%FromJSON(tHTTPResponse.Data)
    set pResponse = ##class(osuwmc.COM.Response.StringResponse).%New()
    if responseData.count =1{
      set pResponse.COMPortalURL = responseData.items.%Get(0).portalUrl
      set pResponse.COMPureID = responseData.items.%Get(0).pureId
      set pResponse.COMTitle = responseData.items.%Get(0).titles.%Get(0).value
    }
  }

for the following JSON response...

 

JSON

{
    "count": 1,
    "pageInformation": {
        "offset": 0,
        "size": 10
    },
    "items": [
        {
            "pureId": xxxxxxxxx,
            "uuid": "xxxxxxxxxxxxxxxx",
            "createdBy": "root",
            "createdDate": "2019-01-30T16:41:49.307Z",
            "modifiedBy": "root",
            "modifiedDate": "2025-05-27T23:47:13.319Z",
            "portalUrl": xxxxxxxxxxxxxxxxxxxxxxx",
            "prettyUrlIdentifiers": [
                "xxxxxxxxxxxx"
            ],
            "version": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "name": {
                "firstName": "xxxxxxxxd",
                "lastName": "xxxxxxxxo"
            },
            "staffOrganizationAssociations": [
                {
                    "typeDiscriminator": "xxxxxxxxxxxxxxxxxxxxxxxxx",
                    "pureId": xxxxxxxxxxxxxx,
                    "employmentType": {
                        "uri": "/dk/atira/pure/person/employmenttypes/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    },
                    "organization": {
                        "systemName": "Organization",
                        "uuid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    },
                    "period": {
                        "startDate": "1920-01-01"
                    },
                    "primaryAssociation": false,
                    "staffType": {
                        "uri": "/dk/atira/pure/person/personstafftype/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    }
                },
                {
                    "typeDiscriminator": "StaffOrganizationAssociation",
                    "pureId": xxxxxxxxxxxxxxx
                    "employmentType": {
                        "uri": "/dk/atira/pure/person/employmenttypes/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    },
                    "organization": {
                        "systemName": "Organization",
                        "uuid": "xxxxxxxxxxxxxxxxxxxx"
                    },
                    "period": {
                        "startDate": "1920-01-01"
                    },
                    "primaryAssociation": false,
                    "staffType": {
                        "uri": "/dk/atira/pure/person/personstafftype/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    }
                },
                {
                    "typeDiscriminator": "StaffOrganizationAssociation",
                    "pureId": xxxxxxxxx,
                    "employmentType": {
                        "uri": "/dk/atira/pure/person/employmenttypes/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    },
                    "organization": {
                        "systemName": "Organization",
                        "uuid": "xxxxxxxxxxxxxxxxxxxxxxx"
                    },
                    "period": {
                        "startDate": "1920-01-01"
                    },
                    "primaryAssociation": false,
                    "staffType": {
                        "uri": "/dk/atira/pure/person/personstafftype/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    }
                },
                {
                    "typeDiscriminator": "StaffOrganizationAssociation",
                    "pureId": xxxxxxxx
                    "employmentType": {
                        "uri": "/dk/atira/pure/person/employmenttypes/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    },
                    "organization": {
                        "systemName": "Organization",
                        "uuid": "xxxxxxxxxxxxxxxxxxxxx"
                    },
                    "period": {
                        "startDate": "1920-01-01"
                    },
                    "primaryAssociation": false,
                    "staffType": {
                        "uri": "/dk/atira/pure/person/personstafftype/academic",
                        "term": {
                            "en_US": "Academic"
                        }
                    }
                }
            ],
            "selectedForProfileRefinementService": true,
            "gender": {
                "uri": "/dk/atira/pure/person/gender/unknown",
                "term": {
                    "en_US": "Unknown"
                }
            },
            "titles": [
                {
                    "pureId": xxxxxxx,
                    "value": {
                        "en_US": "Professor"
                    },
                    "type": {
                        "uri": "/dk/atira/pure/person/titles/designation",
                        "term": {
                            "en_US": "Designation"
                        }
                    }
                }
            ],
            "visibility": {
                "key": "FREE",
                "description": {
                    "en_US": "Public - No restriction"
                }
            },
            "identifiers": [
                {
                    "typeDiscriminator": "PrimaryId",
                    "idSource": "synchronisedPerson",
                    "value": "xxxxxxxxxx"
                },
                {
                    "typeDiscriminator": "ClassifiedId",
                    "pureId": xxxxxxx
                    "id": "xxxxxxxx
                    "type": {
                        "uri": "/dk/atira/pure/person/personsources/employee",
                        "term": {
                            "en_US": "Employee ID"
                        }
                    }
                },
                {
                    "typeDiscriminator": "ClassifiedId",
                    "pureId": xxxxx,
                    "id": "xxxxxx",
                    "type": {
                        "uri": "/dk/atira/pure/person/personsources/scopusauthor",
                        "term": {
                            "en_US": "Scopus Author ID"
                        }
                    }
                }
            ],
            "customDefinedFields": {},
            "systemName": "Person"
        }
    ]
}

I am able to pull the responseData.items.%Get(0).portalUrl and responseData.items.%Get(0).pureId, but I am struggling on how to pull out...

"titles": [
                {
                    "pureId": xxxxxxx,
                    "value": {
                        "en_US": "Professor"
                    },
                    "type": {
                        "uri": "/dk/atira/pure/person/titles/designation",
                        "term": {
                            "en_US": "Designation"
                        }
                    }
                }
            ],

Where I would like to pull value.en_US value, "Professor". 

'When I tried set pResponse.COMTitle = responseData.items.%Get(0).titles.%Get(0).value.%Get(0).en_US, I am not getting any data back.

When set pResponse.COMTitle = responseData.items.%Get(0).titles.%Get(0).value I get back 95@%Library.DynamicObject.

What is the correct syntax?

Product version: IRIS 2024.1
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2024.1 (Build 267_2U) Tue Apr 30 2024 16:06:39 EDT [HealthConnect:3.5.0-1.m1]

Comments

Brett Saviano · May 28

Try responseData.items.%Get(0).titles.%Get(0).value.%Get("en_US") or responseData.items.%Get(0).titles.%Get(0).value."en_US".

0
Scott Roth  May 28 to Brett Saviano

Thanks responseData.items.%Get(0).titles.%Get(0).value.%Get("en_US") did the trick

0
Ashok Kumar T · May 28

As always there is a possibility to get <INVALID OREF> while direct access of objects. So, we can use responseData.items.%Get(0).titles.%Get(0).value.%Get("en_US") with some additional checks like below.

If$IsObject(responseData.items) && (responseData.items.%Size()) {
    dao1 =responseData.items.%Get(0) 
    If$IsObject(dao1.titles) {
        dao1.titles.%Get(0).value.%Get("en_US")
    }
}
0
Eduard Lebedyuk  May 28 to Ashok Kumar T

You can also use defaults for that:

w responseData.%Get("items",[]).%Get(0,{}).%Get("titles",[]).%Get(0, {}).%Get("value",{}).%Get("en_US")
0
Eduard Lebedyuk · May 28

Check this example. It iterates all JSON elements, and also outputs the corresponding paths to access them.

0
Dmitry Maslennikov · May 28

There is one more way of getting it, is using ASQ

USER> write responseData.apply("$.items[0].titles[0].value.en_US").%Get(0)

Professor
0