Restful API - Nested JSON objects

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
gillettd
Posts: 19
Joined: Tue May 24, 2016 2:13 pm

Restful API - Nested JSON objects

Post 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
Catt
Posts: 8
Joined: Mon Jun 10, 2019 8:00 pm

Re: Restful API - Nested JSON objects

Post 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.
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Restful API - Nested JSON objects

Post by Dino »

The new (free) courses are here in learn.lansa.com:
learnapi.png
learnapi.png (146.29 KiB) Viewed 12006 times
https://learn.lansa.com/courses/lansa-v ... stful-apis
https://learn.lansa.com/courses/get-started-with-apis
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Restful API - Nested JSON objects

Post 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 11964 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
Attachments
QuickExport20220725140318.zip
(9.18 KiB) Downloaded 1256 times
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Restful API - Nested JSON objects

Post 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
Post Reply