Object definition in API and read multiple object from JSON

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
naek
Posts: 5
Joined: Wed Dec 13, 2023 6:31 pm

Object definition in API and read multiple object from JSON

Post by naek »

Hello all,

I hope everyone is doing well :)

I'm currently working on an API project to externalize Micro Services.

1- Regarding the GET method, I was able to create an API that retrieves results from multiple tables and returns the JSON in the following format:
Example:
{
"level1": {
"level2": "Value at level 2",
"level2": "Value at level 2"
}
}
My schema is :

-
Get définition.png
Get définition.png (13.72 KiB) Viewed 10617 times
The issue here is that if I have 3 levels:
{
"level1": {
"level2": {
"level3": "Value at level 3"
}
}
}
I'm unable to get the correct JSON result; the array created for level 3 is always empty.

2- Concerning the POST method, I want to insert all 3 levels of a JSON that I receive. After declaring the 3 levels in LANSA, the insertion is only done for the first element of the JSON, in my case here just the Level 1 is inserted into the database.

I'm not sure if I'm overlooking something in my code, misunderstanding how to define objects, or if this task simply cannot be accomplished with Lansa :(

Thanks to everyone who took the time to read my post. :)

Best Regards,
KEC
Posts: 12
Joined: Fri Jun 04, 2021 4:58 pm

Re: Object definition in API and read multiple object from JSON

Post by KEC »

The trick is to use the .Add for the initial items. This is covered in Learn LANSA - Modern API's - Chapter 6.

In the API Definition, create 6 types.
Level1Type - Object - this is the most inner level
Level1Array - Array - Reference Level1Type
Level2Type - Object - Includes reference to Level1Array
Level2Array - Array - Reference Level2Type
Level3Object - Object - Includes reference to Level2Array
Level3Array - Array. This is the most outer level

Then have 3 embedded select/endselect which populates.

Here is a sample based on the xCars table shipped with LANSA

Code: Select all

Srvroutine Name(GetMakesModelCar) Response(*HTTP #uHTTP)
  Define_Com Class(#com_home.GetMakeModelCar) Name(#uOperation)
  Define_Com Class(#com_home.MakeArray) Name(#uMakeArray)
  Define_Com Class(#com_home.ModelArray) Name(#uModelList)
  Define_Com Class(#com_home.ModelType) Name(#uModelTemp) Reference(*DYNAMIC)
  Define_Com Class(#com_home.MakeType) Name(#uMakeTemp) Reference(*DYNAMIC)

  Define_Com Class(#xModelId) Name(#uModelID)

  If (#uOperation.TryBind( #uHTTP ))
    Select Fields(#xMakeId #xMakeDescription) From_File(xCarMakes)
      #uMakeArray.Add Result(#uMakeTemp)
      Set Com(#uMakeTemp) Com_Fields(#xMakeId #xMakeDescription)

      Select Fields(#xModelId #xModelDescription) From_File(xCarModels) With_Key(#xMakeId)
        #uMakeTemp.Models.Add Result(#uModelTemp)
        Set Com(#uModelTemp) Com_Fields(#xModelId #xModelDescription)

        #uModelID := #xModelId
        * Bad indexs on xCars.  no Make/Model index
        Select Fields(#xCarVIN #xCarYear #xCarPriceOnSale) From_File(xCarsByMakeId) Where(#uModelID = #xModelId) With_Key(#xMakeId)
          Add_Entry To_List(#uModelTemp.cars)
        Endselect
      Endselect
    Endselect
    If (#uOperation.CheckIoStatus( #IO£STS ))
      #uOperation.Response.ContentJson <= #uMakeArray
    Endif
  Endif
Endroutine
Post Reply