Page 1 of 3

Help With JSON formatting

Posted: Wednesday 10th August 2022 9:51am
by AndyGable
Hi Everyone

I need some help on the following

I am sending the following JSON to a web API that i use in Windows

Code: Select all

Dim jsonString As String = "{""transactionType"":""SALE"",""amount"":""" & transAmount & """,""currency"":""GBP""}"
and in Windows this works fine but in Gambas none of the extra " are sent so I receive back from the API "Invalid JSON Received"

could someone show me how I can send the JSON to the API correctly

this is the code I am using to send to the API

Code: Select all

Dim hClient As HttpClient
    Dim sBuffer As String

    Dim jsonString As String = "{" "transactionType" ":" "SALE" "," "amount" ":" "" & transAmount & "" "," "currency" ":" "GBP" "}"

   message(JSON.Encode(jsonString))
   
    hClient = New HttpClient As "hClient"

    With hClient
        .URL = global.PS_URL & "/terminals/" & global.terminalIDNumber & "/transactions"
        .Auth = 1
        .User = Global.PS_USER
        .Password = Global.PS_PASS
       ' .Headers.Add("Software-House-ID :SD45T92")
       ' .Headers.Add("Installer-Id: " & Global.InstallerID)
        .Async = False
        .Timeout = 60
        .Post("application/connect.v2+json", JSON.Encode(jsonString))
    End With

    global.showResult("POST", hClient)
as yes as you can all see i am still struggling to work out how to send the extra 2 Headers to the API as well (i need to send them so the API would work at 100% service)

Re: Help With JSON formatting

Posted: Thursday 11th August 2022 12:21pm
by PJBlack
escape the quotation mark with \

string = " \"this should work \""

Re: Help With JSON formatting

Posted: Thursday 11th August 2022 12:27pm
by AndyGable
PJBlack wrote: Thursday 11th August 2022 12:21pm escape the quotation mark with \

string = " \"this should work \""
Dim jsonString As String = "{\" \"transactionType\" \":\" \"SALE\" \",\"\"amount\" \":\" \"\" & transAmount & \"\" \",\" \"currency\" \":\" \"GBP\" \"}"

Something like that?

Re: Help With JSON formatting

Posted: Saturday 13th August 2022 9:39am
by PJBlack
don't know your json but if you like to have a quotation mark inside a string you have to escape it with a backslash ...

OR

something like so:
string = "first part" & chr(34) & "second part"
will result in: firstpart"second part

Re: Help With JSON formatting

Posted: Sunday 14th August 2022 10:01am
by AndyGable
Thanks PJBlack

I shall try that and let you know what the status message is from the cloud server

Re: Help With JSON formatting

Posted: Sunday 14th August 2022 4:03pm
by AndyGable
Hi Everyone

I have tired the corrections as you all said and I am still getting a Error from the API

I have even tried this bit of code

Code: Select all

 Dim hClient As HttpClient
    Dim sBuffer As String
'   Dim jsonString As String = "{""transactionType"":""SALE"",""amount"":""" & transAmount  & """,""currency"":""GBP""}"
    Dim jsonString As String = "{¬¬transactionType¬¬:¬¬SALE¬¬,¬¬amount¬¬:¬¬" & transAmount & "¬¬¬,¬¬currency¬¬:¬¬GBP¬¬}"
    
    jsonString = Replace(jsonString, "¬", Chr(34))
    
    message(JSON.Encode(jsonString))
   
    hClient = New HttpClient As "hClient"

    With hClient
        .URL = global.PS_URL & "/terminals/" & global.terminalIDNumber & "/transactions"
        .Auth = 1
        .User = Global.PS_USER
        .Password = Global.PS_PASS
       ' .Headers.Add("Software-House-ID :SD45T92")
       ' .Headers.Add("Installer-Id: " & Global.InstallerID)
        .Async = False
        .Timeout = 60
        .Post("application/connect.v2+json", JSON.Encode(jsonString))
    End With

    global.showResult("POST", hClient)
as you can see I replaced every " with a ¬ with in the string formation and then did a replace function at the end to get the format correct

This is what I am getting on the message box

Code: Select all

"{\" \"transactionType\" \":\" \"SALE\" \",\" \"amount\" \":\" \"\"153\"\"\",\" \"currency\" \":\" \"GBP\" \"}"
But I am still getting a Error from the card Server API I get in my list box

{"messages":{"error":["Invalid JSON received."]}}

so I am completely stumped as to what to try now I have even asked the Card Service providers as to what they are getting their end but I have not had a replay back yet.

Does anyone know what I have done wrong?

Re: Help With JSON formatting

Posted: Sunday 14th August 2022 7:17pm
by grayghost4
I know nothing about what your are doing

Do you want the 153 to be double quoted ? and there are spaces between each element except the 153 and the comma following it has no space.

If it does require double quotes .... does it need spaces between the quotes ?

Re: Help With JSON formatting

Posted: Sunday 14th August 2022 10:01pm
by PJBlack
try this one :
Dim jsonString As String = "{\"transactionType\":\"SALE\",\"amount\":\"" & transAmount & \"",\"currency\":\"GBP\"}"

Re: Help With JSON formatting

Posted: Sunday 14th August 2022 10:28pm
by AndyGable
hi PJBlack

I am getting Unexpected "\" and it is flashing next to the & \ just after the amount

so it is showing transAmount & \ "", it is the \ i am getting the error on

Sorry if I am not any help I am still figuring this out as I go along.

Re: Help With JSON formatting

Posted: Monday 15th August 2022 3:08am
by grayghost4
Try this :
  Dim transAmount As String = "2345.45"
  Dim TraType As String = "SALE"
   Dim jsonString As String = "{\"transactionType\":" & Quote(TraType) & ",\"amount\":" & Quote(transAmount) & ",\"currency\":\"GBP\"}" 
  
It produces this:
{"transactionType":"SALE","amount":"2345.45","currency":"GBP"}