Page 1 of 1

Restful API - Nested JSON objects

Posted: Thu Jul 21, 2022 6:32 pm
by gillettd
Hi,

Is there any doco on how to return nested json with restful api

e.g

{
"isbn": "123-456-222",
"author":
{
"lastname": "Doe",
"firstname": "Jane"
},
"editor":
{
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": ["Non-Fiction", "Technology"]
}

or nested array

{

"isbn": "123-456-222",
"authors":
[
{
"lastname": "Doe",
"firstname": "Jane"
},
{
"lastname": "Smith",
"firstname": "Jane"
}
],

"title": "The Ultimate Database Study Guide",

}


Cheers,

Dominique

Re: Restful API - Nested JSON objects

Posted: Fri Jul 22, 2022 1:26 am
by Catt
Is this V15 you're creating the API in? If so there are some tutorials on Learn LANSA. I've recently been working on some APIs that are nested so if that isn't sufficient I can provide some examples of how to do this.

Re: Restful API - Nested JSON objects

Posted: Sat Jul 23, 2022 1:35 am
by Dino
The new (free) courses are here in learn.lansa.com:
learnapi.png
learnapi.png (146.29 KiB) Viewed 12005 times
https://learn.lansa.com/courses/lansa-v ... stful-apis
https://learn.lansa.com/courses/get-started-with-apis

Re: Restful API - Nested JSON objects

Posted: Thu Jul 28, 2022 3:57 am
by Dino
Hi Dominique

If you want to create a more complex and unstructured json, maybe creating all the schemas and objects is harder, so it is easier just the create it in the code like this:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)
Srvroutine Name(booksRoutine) Response(*HTTP #http)
Define_Com Class(#Com_Home.getBookDetails) Name(#Operation)
Define Field(#contentstring) Type(*String) Length(65535)
Define_Com Class(#XPRIM_JsonWriter) Name(#Writer)
Define_Com Class(#XPRIM_JsonObject) Name(#ChildObject)
Define_Com Class(#XPRIM_JsonArray) Name(#VarArray) Reference(*Dynamic)

If (#Operation.TryBind( #http ))
#Writer.SetOutputToString

#Writer.BeginObject

#Writer.WriteString Name('isbn') Value("123-456-222")

#Writer.BeginArray Name('authors')
Select Fields(#surname #givename) From_File(pslmst1) With_Key("INF")
#ChildObject.InsertString Key('lastname') String(#SURNAME)
#ChildObject.InsertString Key('firstname') String(#GIVENAME)
#Writer.WriteElement Name("xyz") Value(#ChildObject)
Endselect
#Writer.EndArray

#Writer.WriteString Name('title') Value("The Ultimate Database")

#Writer.EndObject

#contentstring := #Writer.AsString.AsNativeString

#Operation.Response.SetContentString( #contentstring )
Endif
Endroutine
End_Com
which produces this result:

Code: Select all

{
  "isbn": "123-456-222",
  "authors": [
    {
      "lastname": "Turner",
      "firstname": "Valerie"
    },
    {
      "lastname": "Neave",
      "firstname": "Gazza"
    },
    {
      "lastname": "Lincoln",
      "firstname": "Paul"
    },
    {
      "lastname": "Brown",
      "firstname": "Veronica"
    }
  ],
  "title": "The Ultimate Database"
}
and for this, I created my server module with this API definition (attached here as a quick export as well)
contentstringarray.png
contentstringarray.png (66.55 KiB) Viewed 11963 times
you can test it using this URL and get:
http://localhost:8080/rho/r727/Book

docs here:
https://docs.lansa.com/15/en/LANSA015/# ... 1_0015.htm

Re: Restful API - Nested JSON objects

Posted: Thu Jul 28, 2022 4:10 am
by Dino
Alternative version for the server module, just using XPRIM_JsonWriter (suppose to be faster according to the doc):

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)
Srvroutine Name(booksRoutine) Response(*HTTP #http)
Define_Com Class(#Com_Home.getBookDetails) Name(#Operation)
Define Field(#contentstring) Type(*String) Length(65535)
Define_Com Class(#XPRIM_JsonWriter) Name(#Writer)

If (#Operation.TryBind( #http ))
#Writer.SetOutputToString

#Writer.BeginObject

#Writer.WriteString Name('isbn') Value("123-456-222")

#Writer.BeginArray Name('authors')
Select Fields(#surname #givename) From_File(pslmst1) With_Key("INF")
#Writer.BeginObject
#Writer.WriteString Name('lastname') Value(#SURNAME)
#Writer.WriteString Name('firstname') Value(#GIVENAME)
#Writer.EndObject
Endselect
#Writer.EndArray

#Writer.WriteString Name('title') Value("The Ultimate Database")

#Writer.EndObject

#contentstring := #Writer.AsString.AsNativeString

#Operation.Response.SetContentString( #contentstring )
Endif
Endroutine
End_Com