Question
· Apr 17

Iterate on array in a json structure

Hi,

i have this simple json structure:

{
"nTypeTrigger": "ATR",
"sDate": "2024-04-17 15:29:16",
"tRefArray": [{"sID":"132"},{"sID":"151"},{"sID":"233"}],
"tCountries": []
}

I can't find an example to iterate on tRefArray.

I've tried to create a secondary iterator but it doesn't work. Here is my current code:

 

 

 // extract json content from the request:
 set dynRequestJsonPayload = {}.%FromJSON(%request.Content)
 set JsonIterator = dynRequestJsonPayload.%GetIterator()
 
 // iterate on json structure:
 if dynRequestJsonPayload '= "" {
    while JsonIterator.%GetNext(.key, .value) { 
      set NodeType = dynRequestJsonPayload.%GetTypeOf(key)

      if NodeType = "string" {
        do GlobalTrace("NodeType: " _ NodeType _ "; key: " _ key _ "; value: " _ value)
      }

      if NodeType = "array" {
         // i want to iterate on this array...
         // the following line throws the exeception "ERREUR #5002: Erreur ObjectScript: <INVALID OREF>traitementUFI+34^common.REST.1"
         set JsonIteratorSecondary = key.%GetIterator()

      }

    }
}
 

Thanks for help.

Discussion (3)1
Log in or sign up to continue

You need to iterate on value:

 // extract json content from the request:
 set dynRequestJsonPayload = {}.%FromJSON(%request.Content)
 #dim JsonIterator As %Iterator.AbstractIterator
 set JsonIterator = dynRequestJsonPayload.%GetIterator()
 
 // iterate on json structure:
 if dynRequestJsonPayload '= "" {
    while JsonIterator.%GetNext(.key, .value, .NodeType) { 
      if NodeType = "string" {
        do GlobalTrace("NodeType: " _ NodeType _ "; key: " _ key _ "; value: " _ value)
      } elseif NodeType = "array" {
         // i want to iterate on this array...
         // the following line throws the exeception "ERREUR #5002: Erreur ObjectScript: <INVALID OREF>traitementUFI+34^common.REST.1"
         set JsonIteratorSecondary = value.%GetIterator()
      } else {
	      // Do something
      }

    }
}