Help With JSON formatting

Post your Gambas programming questions here.
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Help With JSON formatting

Post 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)
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Help With JSON formatting

Post by PJBlack »

escape the quotation mark with \

string = " \"this should work \""
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help With JSON formatting

Post 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?
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Help With JSON formatting

Post 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
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help With JSON formatting

Post by AndyGable »

Thanks PJBlack

I shall try that and let you know what the status message is from the cloud server
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help With JSON formatting

Post 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?
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Help With JSON formatting

Post 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 ?
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Help With JSON formatting

Post by PJBlack »

try this one :
Dim jsonString As String = "{\"transactionType\":\"SALE\",\"amount\":\"" & transAmount & \"",\"currency\":\"GBP\"}"
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help With JSON formatting

Post 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.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Help With JSON formatting

Post 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"}
Post Reply