Page 1 of 1

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

Posted: Mon Aug 16, 2021 9:15 pm
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?

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

Posted: Tue Aug 24, 2021 6:07 pm
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.

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

Posted: Tue Dec 07, 2021 11:22 am
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