using #PRIM_JSON.Document to save json request and response data

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
PoojaSingh
Posts: 3
Joined: Sat Aug 14, 2021 8:27 am

using #PRIM_JSON.Document to save json request and response data

Post by PoojaSingh »

Hi,

I am looking for more information about #PRIM_JSON.Document, more specifically about its SaveToFile feature.
I need to store request being sent to API and response coming back from API in a JSON format file. Currently both the request and response data is in the form of #PRIM_DC.Unicode string.
I am able to load string to #PRIM_JSON.Document type object but how do I save this document in a particular folder/directory?

Code sample:

Mthroutine Name(SaveRequest)
Define_Map For(*INPUT) Class(#STD_A50) Name(#Filename)
Define_Map For(*INPUT) Class(#PRIM_DC.UnicodeString) Name(#RequestStr)
Define_Map For(*INPUT) Class(#STD_A512) Name(#InputUrl)
Define_Com Class(#XPRIM_JsonWriter) Name(#DocWriter)
Define_Com Class(#PRIM_DC.UnicodeString) Name(#Text)
Define_Com Class(#PRIM_JSON.Document) Name(#Doc)

#DocWriter.SetOutputToString String(#Text)
#DocWriter.BeginObject
#DocWriter.WriteString Name('URL') Value(#InputUrl)
#DocWriter.WriteString Name('RequestString') Value(#RequestStr.AsNativeString)
#DocWriter.EndObject
#Doc.LoadFromString Inputstring(#Text.AsNativeString)
#Filename := *DD_XML_PATH + #Filename
#Doc.SaveToFile // not sure how to use it :(
Endroutine


Could you please help me understand how do I save #Doc data to a particular location in json format?
Is there any other approach to save json file in lansa?
Dominik
Posts: 21
Joined: Fri Jun 10, 2016 12:14 pm

Re: using #PRIM_JSON.Document to save json request and response data

Post by Dominik »

Hi Pooja, the PRIM_JSON.Document SaveToFile method expects a TextWriter reference to be passed to it. This is a #PRIM_IOC.StreamWriter, which itself needs to have the Stream set to a PRIM_IOC.FileStream

Code: Select all

Define_Com Class(#prim_ioc.FileStream) Name(#OutputStream) Fileaccess(Write) Filemode(CreateNew) Fileshare(Write)
Define_Com Class(#prim_ioc.StreamWriter) Name(#StreamWriter) Stream(#OutputStream)
You can set the filename output by the filestream at runtime:

Code: Select all

#OutputStream.Path := #Filename
#Doc.SaveToFile( #StreamWriter )
Note that you could have just set the #DocWriter.SetOutputToFile and specified a path in your sample.
PoojaSingh
Posts: 3
Joined: Sat Aug 14, 2021 8:27 am

Re: using #PRIM_JSON.Document to save json request and response data

Post by PoojaSingh »

Hi Dominik,

I apologize for this late reply.

Thank you very much for your help. It is greatly appreciated.

Below is the snippet I used to save json as a file

#Filename := *DD_XML_PATH + '\' + #Filename
#DocWriter.SetOutputToFile Path(#Filename)
#DocWriter.BeginObject
#DocWriter.WriteString Name('URL') Value(#InputUrl)
#DocWriter.WriteString Name('RequestString') Value(#Payload.AsNativeString)
#DocWriter.EndObject
Post Reply