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?
Comments
Try responseData.items.%Get(0).titles.%Get(0).value.%Get("en_US") or responseData.items.%Get(0).titles.%Get(0).value."en_US".
Thanks responseData.items.%Get(0).titles.%Get(0).value.%Get("en_US") did the trick
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")
}
}You can also use defaults for that:
w responseData.%Get("items",[]).%Get(0,{}).%Get("titles",[]).%Get(0, {}).%Get("value",{}).%Get("en_US")Check this example. It iterates all JSON elements, and also outputs the corresponding paths to access them.
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