Question
Sudarshan Kumar · Jan 12, 2018

Read values from JSON in %CSP.Request

(I am new to this language) I have got a JSON request to handle in a web application which is in %CSP.Request object. But the request which I am going to handle is of type array of objects.
like,

[
  {
    "Name": "bat"
  },
  {
    "Name": "Cat"
  },
  {
    "Name": "rat"
  },
  {
    "Name": "mat"
  },
  {
    "Name": "hat"
  },
  {
    "Name": "chat"
  },
  {
    "Name": "please"
  },
  {
    "Name": "help"
  },
  {
    "Name": "me"
  },
  {
    "Name": "in"
  },
  {
    "Name": "getting"
  },
  {
    "Name": "the"
  },
  {
    "Name": "value"
  },
  {
    "Name": "of"
  },
  {
    "Name": "nameFromObjectInsideArray"
  }
]

my requirement is, i need to extract the value from the " Name " property which is an array of objects.

00
1 0 10 1,508
Log in or sign up to continue

I will give my requirement as it is.

I am getting an json array of Roles object, in which only "name" property is sent. I have to see if these names are present in DB or not. In case there is a new name in the json, then i have to pick that value and save it in DB.

version - 2015.1.OHS.3703.0

i have a json request as follows:

[  
   {  
      "Name":"Billing"
   },
   {  
      "Name":"Lead"
   },
   {  
      "Name":"Coordinator"
   },
   {  
      "Name":"Director"
   },
   {  
      "Name":"Main"
   },
   {  
      "Name":"Marketing"
   },
   {  
      "Name":"Workshop"
   },
   {  
      "Name":"In-House Trainer"
   },
   {  
      "Name":"Committee"
   },
   {  
      "Name":"Facilitator"
   },
   {  
      "Name":"Facilitator"
   }
  ]

Now my code : Controller

ClassMethod CheckMissingRoleName() As %Status
{

#dim newObj As Model.Role
#dim %request As %CSP.Request
#Dim MyStrings As %ListOfObjects 

set inputParms %request.Content
set MyStrings inputParms.data

Set resultu=##class(%ResultSet).%New("%DynamicQuery:SQL")
  Do resultu.Prepare("select Name from Model.Role ")  // 
Do resultu.Execute()
set i=1
while (resultu.Next())
{
set namesArray(i) = resultu.Data("Name")
set i=i+1
  //write namesArray(i)
}

for k=1:1:MyStrings.Size 

for j=1:1:i
{
if MyStrings.GetAt(k).Name '= namesArray(j) ) }
else {  break  }
}
// i is size of array and j is size of above loop i.e coldn't find match, and reached the end
if (j)
{
   set newObj ##class(Model.Role.%OpenId(MyStrings.GetAt(k).ID)
 set newObj.Name=MyStrings.GetAt(k).Name
 // write MyStrings.GetAt(k)
 Do newObj.%Save()
    }
}
return $$$OK
}

After sending the request using Postman, i am getting this error:

{
    "Error": "ERROR #5002: Cache error: <PROPERTY DOES NOT EXIST>zCheckMissingRoleName+2^Controllers.Roles.2 *data,%Library.ListOfObjects"
}

 

and one more thing, how do i print something on the console? Just for debugging

I will give my requirement as it is.

I am getting an json array of Roles object, in which only "name" property is sent. I have to see if these names are present in DB or not. In case there is a new name in the json, then i have to pick that value and save it in DB.

version - 2015.1.OHS.3703.0

i have a json request as follows:

[  
   {  
      "Name":"Billing"
   },
   {  
      "Name":"Lead"
   },
   {  
      "Name":"Coordinator"
   },
   {  
      "Name":"Director"
   },
   {  
      "Name":"Main"
   },
   {  
      "Name":"Marketing"
   },
   {  
      "Name":"Workshop"
   },
   {  
      "Name":"In-House Trainer"
   },
   {  
      "Name":"Committee"
   },
   {  
      "Name":"Facilitator"
   },
   {  
      "Name":"Facilitator"
   }
  ]

Now my code : Controller

ClassMethod CheckMissingRoleName() As %Status
{

#dim newObj As Model.Role
#dim %request As %CSP.Request
#Dim MyStrings As %ListOfObjects 

set inputParms = %request.Content
set MyStrings = inputParms.data

Set resultu=##class(%ResultSet).%New("%DynamicQuery:SQL")
  Do resultu.Prepare("select Name from Model.Role ")  // 
Do resultu.Execute()
set i=1
while (resultu.Next())
{
set namesArray(i) = resultu.Data("Name")
set i=i+1
  //write namesArray(i)
}

for k=1:1:MyStrings.Size 

for j=1:1:i
{
if ( MyStrings.GetAt(k).Name '= namesArray(j) ) }
else break  }
}
// i is size of array and j is size of above loop i.e coldn't find match, and reached the end
if (= j)
{
   set newObj = ##class(Model.Role.%OpenId(MyStrings.GetAt(k).ID)
 set newObj.Name=MyStrings.GetAt(k).Name
 // write MyStrings.GetAt(k)
 Do newObj.%Save()
    }
}
return $$$OK
}

After sending the request using Postman, i am getting this error:

{
    "Error": "ERROR #5002: Cache error: <PROPERTY DOES NOT EXIST>zCheckMissingRoleName+2^Controllers.Roles.2 *data,%Library.ListOfObjects"
}

 

and one more thing, how do i print something on the console? Just for debugging

Thanks for responding.

while i was using your code, i am getting this error in response, which says "read" method is unknown

{
    "Error": "ERROR #5002: Cache error: <METHOD DOES NOT EXIST>zCheckMissingRoleName+3^Controllers.Roles.2 *Read,%Library.ListOfObjects"
}

If just take out that "read method", then i am getting this error

{
    "Error": "ERROR #5002: Cache error: <INVALID OREF>zCheckMissingRoleName+16^Controllers.Roles.3"
}

while i was using your code, i am getting this error in response, which says "read" method is unknown

You're getting this error because list of objects does not have this method. Check your list of objects for contents. Seems like json is already converted so you don't need to do that.

 

 

Thanks for responding patiently. Problem is solved.

May i know how to compare two strings for equality.  

like,

"inter" = "inter"  (  true ) 

"black"="white " (  false )

"inter system"="inter sy"  ( false )

"inter systems"="inter systems" ( true )

As you can see from above that it should be an exact match. Is there any method to do so with String.

Why not just use the "=" operator to compare your values, exactly as you have written in your examples?

Convert json to proxy object (list of in your case) and iterate over it

set json = "[{""Name"":""bat""},{""Name"":""Cat""},{""Name"":""rat""},{""Name"":""mat""},{""Name"":""hat""},{""Name"":""chat""},{""Name"":""please""},{""Name"":""help""},{""Name"":""me""},{""Name"":""in""},{""Name"":""getting""},{""Name"":""the""},{""Name"":""value""},{""Name"":""of""},{""Name"":""nameFromObjectInsideArray""}]"
set sc = ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(json,,.obj,1)
for i=1:1:obj.Count() { 
    write obj.GetAt(i).Name,!
}

To get json string from  request:

set json = %request.Content.Read($$$MaxStringLength)

You may also need to convert request into UTF8:

set json = $ZCVT(json,"I","UTF8")