Iterate the fields in a JsonObject

Posted by Ken Ward on 13-Apr-2017 09:32

I'm trying to parse a JSON string that looks like this:

{
 "success":false,
 "response_code":1,
 "status_message":"One or more errors has occurred.",
 "errors": {
  "45":["Please provide valid Checking Account Number"],
  "46":["Please provide valid Transit Routing Number"],
  "35":["Please provide a valid Credit Card Number"],
  "43":["Please provide a valid Expiration Date"]
 }
}

I'm having issues with the "errors" object.

I don't want  to call jsonObject:GetArray() for every possible number that may be in there.

Is there a way to iterate through the contents of this object?

Thanks

Posted by onnodehaan on 13-Apr-2017 10:16

Hi Ken,

Here is a (incomplete, sloppy) snippet Ithat gives you a general idea about how you could achieve this.

define variable jsonErrors as Progress.Json.ObjectModel.JsonArray.

jsonErrors =  jsonObject:GetJsonArray('errors').

 do iCount = 1 to jsonErrors :length:

   assign lineObj = new Progress.Json.ObjectModel.JsonObject()

              lineObj = jsonErrors:GetJsonObject(iTeller1)

              cNames  = lineObj:getNames().

   do iTeller2 = 1 to extent(cNames):

      cName = cNames[iTeller2].                                 // this gives you: 45, 46, etc,

      cValue = string(lineObj:GetJsonText(cName)).   // this gives you: Please provide valid Checking Account Number, etc

   end.

 end.

All Replies

Posted by onnodehaan on 13-Apr-2017 10:16

Hi Ken,

Here is a (incomplete, sloppy) snippet Ithat gives you a general idea about how you could achieve this.

define variable jsonErrors as Progress.Json.ObjectModel.JsonArray.

jsonErrors =  jsonObject:GetJsonArray('errors').

 do iCount = 1 to jsonErrors :length:

   assign lineObj = new Progress.Json.ObjectModel.JsonObject()

              lineObj = jsonErrors:GetJsonObject(iTeller1)

              cNames  = lineObj:getNames().

   do iTeller2 = 1 to extent(cNames):

      cName = cNames[iTeller2].                                 // this gives you: 45, 46, etc,

      cValue = string(lineObj:GetJsonText(cName)).   // this gives you: Please provide valid Checking Account Number, etc

   end.

 end.

Posted by Ken Ward on 13-Apr-2017 11:57

I rewrote it a little, but it works Thanks!

       DEF VAR cnames   AS CHAR EXTENT NO-UNDO.  

       DEF VAR cMsgs    AS CHAR EXTENT NO-UNDO.

       jErrors  = jsonObj:GetJsonObject("errors").              

       cnames = jErrors:GetNames().

       EXTENT(cMsgs) = EXTENT(cnames).

       DEF VAR i AS INTEGER NO-UNDO.

       DO i = 1 TO EXTENT(cNames):

         cMsgs[i] = jErrors:GetJsonArray(cnames[i]):GetCharacter(1).

       END.

Posted by Peter Judge on 13-Apr-2017 23:30

Look at jsonErrors:GetJsonObject();GetNames() which returns an array of names
[“45”, “46”, “35”, “43”]
 
You can iterator over that array now.
 

Posted by onnodehaan on 14-Apr-2017 01:50

You're welcome

Posted by Ken Ward on 14-Apr-2017 08:41

Thanks everyone. The main issue was that i missed the GetNames() method amongst all the other Get*() Methods listed.

This thread is closed