Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Response txt to Json string 1

Status
Not open for further replies.

DominicProgrammer

IS-IT--Management
May 14, 2015
11
KE
Code:
lcUrl = "[URL unfurl="true"]http://192.168.1.100:5000/EsdApi/deononline/signinvoice"[/URL]  && local pc IP
loXmlHttp = Createobject("Msxml2.ServerXMLHTTP.6.0")
llNull = loXmlHttp.Open( "POST" , lcUrl, .F. ) &&..open connection
loXmlHttp.setRequestHeader("Content-Type","application/json")   && you are sending in JSON
loXmlHttp.setRequestHeader("accept", "application/json")  && you are accepting JSON
llNull = loXmlHttp.Send(lcRequest) &&..send request
lcResponse = loXmlHttp.responseText &&..get response
&&&&&  now await response from TIMS & pick the details
Strtofile(Alltrim(m.lcResponse), [c:\copy-cat\api\responses\]+lcOutFIle, .T.)
jsonString = loXmlHttp.responseText

TEXT TO json noshow

&& how do i get this jsonstring here as a json text file?

ENDTEXT

** the resulting json  file should look like below
*!*	{
*!*	    "statusCode": "0",
*!*	    "status": "SUCCESS",
*!*	    "middlewareInvoiceNumber": "13",
*!*	    "traderSystemInvoiceNumber": "953790",
*!*	    "controlCode": "0100000000000000013",
*!*	    "qrCode": "[URL unfurl="true"]https://tims-test.kra.go.ke/KRA-Portal/invoiceChk.htm?actionCode=loadPage&invoiceNo=0100000000000000013",[/URL]
*!*	    "invoiceType": "Original",
*!*	    "serialNo": "",
*!*	    "totalTaxAmount": "13.79",
*!*	    "totalTaxableAmount": "86.20",
*!*	    "totalInvoiceAmount": "99.99"
*!*	}

Hi Programmers.

I have the above code and i would like to have the "jsonString" converted to json so that i can now serialise it to a cursor, the jsonString is actually a response from a an API post. How can i do this.. your help will be highly appreciated. Thanks in advance

Dominic.
 
Why not use STRTOFILE() function:

STRTOFILE(myString, "C:\path\to\yourfile.txt")

If you just want to output that text to a text file.


Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
how do i get this jsonstring here as a json text file?

You don't. The TEXT/ENDTEXT construction is used to create a variable containing free-format text which is hard-coded in your program. It has nothing to do with text files.

Your JSON is essentially just a character string. You already have code to get that string into a variable (lcResponse). You say that your end product is a cursor. So it should be simple matter to parse the string and copy the various elements into the cursor. At that point, JSON doesn't enter the picture.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
To convert the json response from the API to a cursor it doesn't help you to store it to a file at all.

The json string is what needs to be converted to either an object or a record. The json you show as comment is actually an object with many properties. So to adress it that way, you need a library to parse the json into an object or a cursor. Writing it to a file is not changing it in any way. The other very natural way of working with json is within javascript, but that's overcomplicating things.

So you need to use a library like this one:
Look back into recent threads, there are lots of usage examples already here, besides the documentation of this library itself.

Chriss
 
Your code comments are leading you into wrong assumptions, so let me rephrase the comments of your code, the code already stores the response json into a file. What you want is alredy done, but you don't seem to understand it:

Code:
lcUrl = "[URL unfurl="true"]http://192.168.1.100:5000/EsdApi/deononline/signinvoice"[/URL]  && local pc IP - true, that refers to just the initial part of the URL, you send a request to a local IP address 192.168.1.100
* This addresses something like local hardaware, not a server in the internet, but tht seems to be its interface, all right.

loXmlHttp = Createobject("Msxml2.ServerXMLHTTP.6.0") && create a http request object

llNull = loXmlHttp.Open( "POST" , lcUrl, .F. ) &&..open connection - start a POST request to the URL given above in synchronous mode, which meand when you finally use send() that will wait for the response

loXmlHttp.setRequestHeader("Content-Type","application/json")   && you are sending in JSON - which you state in a header that's sent to the URL at 192.168.1.100 on port 5000.
loXmlHttp.setRequestHeader("Accept", "application/json")  && you are accepting JSON - which you state in this header, strictly it should be with a big A
* it could be unimportant to set this if the response will be JSON anyway, as it always is json, better know the documentation of that device you're addressing

loXmlHttp.Send(lcRequest) &&..send request - as noted above, this is done synchrnously, so the next line will execute after the response is received.

lcResponse = loXmlHttp.responseText &&..get response - Which is possible because of the synchronous usage, otherwise you'd need to wait for loXmlHttp.readyState to become 4.
*Changing that to asynchronous usage is oif no concern here, but notice it could be done and then an application can do anything else while something else waits for the response, for example a timer.
&&&&&  now await response from TIMS & pick the details - this is wrong, the response is already in lcResponse

Strtofile(Alltrim(m.lcResponse), [c:\copy-cat\api\responses\]+lcOutFIle, .T.) && this writes the response into a file, that's what you asked for, the code already does that.
* This requires the varibable lcOutFIle to be set to a filename and c:\copy-cat\api\responses\ must be an existing directory. Your code does not show how lcOutFIle is set 
* or whether it is a parameter, there likely is an error in not setting lcOutFIle or calling the code wrong without the filename.

jsonString = loXmlHttp.responseText && this is obsolete code, you already have that in lcResponse and it doesn't change, once the request response is received.

Now it just depends on what has to come next. If the creation of a file within the c:\copy-cat\api\responses\ directory should cause some other component, perhaps another connected hardware to react to it, then there's nothing more to do, but I'm quite sure you now want to get out the single values from the JSON string.

It would be pretty simple, if you programmed all this in Javascript then oJS = JSONSTRING would simply make oJS an object that this JSONSTRING actually is, when "run" (or evaluated) in JS. And you could get at the properties oJS.traderSystemInvoiceNumber, for example.

Well, as you're obviously using VFP, you're now on a journey to learn something new, either you use that API with a Javascript program to not have the translatkion/parsing hurdle, or you make use of libraries like nfJSON to turn the JSON into a VFP object or cursor.

In the first place, by definition of the acronym JSON a JSON string is Javastrict OBJECT notation, so it is an Object, not a table or record. Libraries like nfJSON are not necessary in Javascript itself, as JSON there is native, it already is what you now need to convert because you use VFP and not Javascript. You're not alone, libraries like this also exist for other languages, like PHP. But you're likely having a totally oversimplified assumption and imagingation of JSON automatically becoming a DBF when you store it to a file. But don't thinkj every JSON is simply turned into the same thing, JSON can encode anything, It's usage of a specific API call like this one in the end requesting a "signinvoice", that gets the JSON representing that.

Just one last comment: Your thread title points out that you want to read a response txt file into a string (1), in your post you ask how to generate a txt file (2), the total opposite. Your code already does 2 and it still has the response string in lcResponse and in jsonString, becasue it does the step to read out the loXmlHttp.responseText property twice into two variables with different names. I don't know what's not working, but if your code is failing it's surely failing on a level you don't yet have understood yourself. If you don't find a file in c:\copy-cat\api\responses\ with current datetime, then the error likely is about not setting lcOutFIle. If this is all code in progress and first time usage, well, then the pointer to the nfJSON library should help you get on with this.

Chriss
 
HI Mike & Chris.

Thank you guys for your valued inputs, I really appreciate it. I have used the Nfjson solution and i can now proceed with the API link & execution.
Dominic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top