API - array in JSON of body of the request

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
RomainG
Posts: 16
Joined: Mon Aug 05, 2024 4:29 pm

API - array in JSON of body of the request

Post by RomainG »

Hi,

I am working on a POST request for an API and in the body of my request I have a JSON object. In this JSON object I have 3 elements, two being fields and one being an array. Example:
{
"NaceArray": ["41","42","43","71"],
"CODLAN": "1",
"NBRMAX": 0
}

In the API source code, if I want to access the data of the JSON object, I did the following:

Code: Select all

#NaceObjectReq <= #Operation.Request.ContentJson

#CodeLangue := #NaceObjectReq.CODLAN
#NbrMax := #NaceObjectReq.NBRMAX
However, I am having trouble assigning the data in the array to a list or a collection so that I can loop in the array. I have tried by defining a working list or a collection of strings and then assigning the array to them but it does not work. Example:

Code: Select all

Def_List Name(#NaceList) Fields(#CODNAC) Type(*WORKING) Entrys(*MAX)
Define_Com Class(#PRIM_LCOL<#STD_ALPHA>) Name(#NaceCol) Reference(*DYNAMIC)
...
#NaceList <= #NaceObjectReq.NaceArray
#NaceCol<= #NaceObjectReq.NaceArray
I have this error: "The class of component #NaceCol is not assignment compatible with #NaceObjectReq.NaceArray".

Also, if I try to loop directly in the array using selectlist or the For Each commands, I have these errors:
selectlist and For Each errors
selectlist and For Each errors
error_loop.png (3.89 KiB) Viewed 17843 times

Although I can access the array data one by one like this: #NaceObjectReq.NaceArray<1>, I would like to know if there is a "cleaner" way to do this.

Thanks for your help,

Romain
adale
Posts: 210
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: API - array in JSON of body of the request

Post by adale »

Romain,
Could you utilize the XPRIM_RandomAccessJsonReader ?
For example, I had a json array in response data to an API post, and read out the four array records as shown. I incremented the int STD_NUM in a CASE statement, but you could process as you see best fit for your needs.
I am sure there are other ways, this just worked for me.

Code: Select all

* Response Status Array
#Reader1.SetSourceString String(#Response_String)

#STD_NUM := 1
#Reader1.BeginArrayWithName Name('ResponseDataResult') Found(#found)
BEGIN_LOOP
#Reader1.BeginObjectAtIndex Index(#STD_NUM) Found(#found)
LEAVE If(#Found.IsFalse)

CASE Of_Field(#STD_NUM)
WHEN Value_Is(*EQ 1)
#ZPBOS01 := #Reader1.ReadStringWithPath( 'Status' ).AsNativeString
#ZPBON01 := #Reader1.ReadStringWithPath( 'LastName' ).AsNativeString
#ZPBON01 := #ZPBON01 + ', ' + #Reader1.ReadStringWithPath( 'FirstName' ).AsNativeString
WHEN Value_Is(*EQ 2)
#ZPBOS02 := #Reader1.ReadStringWithPath( 'Status' ).AsNativeString
#ZPBON02 := #Reader1.ReadStringWithPath( 'LastName' ).AsNativeString
#ZPBON02 := #ZPBON02 + ', ' + #Reader1.ReadStringWithPath( 'FirstName' ).AsNativeString
WHEN Value_Is(*EQ 3)
#ZPBOS03 := #Reader1.ReadStringWithPath( 'Status' ).AsNativeString
#ZPBON03 := #Reader1.ReadStringWithPath( 'LastName' ).AsNativeString
#ZPBON03 := #ZPBON03 + ', ' + #Reader1.ReadStringWithPath( 'FirstName' ).AsNativeString
WHEN Value_Is(*EQ 4)
#ZPBOS04 := #Reader1.ReadStringWithPath( 'Status' ).AsNativeString
#ZPBON04 := #Reader1.ReadStringWithPath( 'LastName' ).AsNativeString
#ZPBON04 := #ZPBON04 + ', ' + #Reader1.ReadStringWithPath( 'FirstName' ).AsNativeString
ENDCASE

#STD_NUM += 1

#Reader1.EndObject

END_LOOP
#Reader1.EndArray
Arlyn Dale
Servias LLC
RomainG
Posts: 16
Joined: Mon Aug 05, 2024 4:29 pm

Re: API - array in JSON of body of the request

Post by RomainG »

Hi Arlyn,

Thanks for your response. I actually found a simpler solution for me to loop in the array:

Code: Select all

Begin_Loop Using(#count) To(#RecNaceObjectReq.NaceArray.Count)
* Process evrything here
End_Loop
Thanks,

Romain
Post Reply