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
Restful API - Nested JSON objects
Re: Restful API - Nested JSON objects
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
The new (free) courses are here in learn.lansa.com:
https://learn.lansa.com/courses/lansa-v ... stful-apis
https://learn.lansa.com/courses/get-started-with-apis
https://learn.lansa.com/courses/lansa-v ... stful-apis
https://learn.lansa.com/courses/get-started-with-apis
Re: Restful API - Nested JSON objects
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:
which produces this result:
and for this, I created my server module with this API definition (attached here as a quick export as well)
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
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
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"
}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 1255 times
Re: Restful API - Nested JSON objects
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