Page 1 of 1

JSON to String but can't escape quotes sanely?

Posted: Sun Mar 29, 2020 4:44 pm
by bmcgonag
I have a POST i want to make through libUrl on LiveCode community edition.

I have a bunch of JSON I have to send as the <data> portion of the POST. The JSON of course must use double quotes, and apparently LiveCode also doesn't like single quotes for a string...

I found that you can replace a literal

Code: Select all

"
with

Code: Select all

& quote
.

I should also mention, that the JSON I'm building needs some items replaced by variable input from the user (e.g. name, IpRangeStart, ipRangeEnd...)

But with this JSON it's starting to look like

Code: Select all

"{" & quote & "config" & quote & ": {" & quote & "name" & quote & ":" & quote & "pickaname" & quote & "," & quote & "ipRangeStarts" & quote & ":" & quote & "192.x.x.x" & quote & "," ...  
So this that I wrote, is just the very beginning of this very long JSON body I need to send.

I hope I'm doing something wrong, and this will actually have some better solution that I'm just not finding in searching online.

I tried escaping by doing

Code: Select all

\"
, but that doesn't work.

So again, any help is greatly appreciated.

Re: JSON to String but can't escape quotes sanely?

Posted: Sun Mar 29, 2020 5:02 pm
by Klaus
Hi bmcgonag,

not sure what you question is, but I use a little function, which saves A LOT of typing in cases like this:

Code: Select all

function q tString
  ## We pass a string and the function will return this string in QUOTES
  return QUOTE & tString & QUOTE
end q
Then you can:

Code: Select all

put "{" & q("config") & q(": {") & q("name") & q(":") & q("pickaname") & q(",") etc...
Hope that helps!

Best

Klaus

Re: JSON to String but can't escape quotes sanely?

Posted: Sun Mar 29, 2020 5:15 pm
by bmcgonag
Ok, I'll give that a shot. Was hoping I had missed a JSONToString() function somewhere....but this should at least shorten it a bit.

Thanks.

Re: JSON to String but can't escape quotes sanely?

Posted: Sun Mar 29, 2020 9:52 pm
by Klaus
There are some mor JSON related handler/functions in LC, but I don't know if they will do what you need.
Enter JSON in the search field of the dictionary and take a look by yourself.

Re: JSON to String but can't escape quotes sanely?

Posted: Sun Mar 29, 2020 10:59 pm
by bmcgonag
Thanks, I looked at the dictionary and the various JSON built in functions.

I guess, my question is, how can I create JSON without an array. Not everything I have starts as an Array. It's just something I'm coding in, and using a couple of user entered variable to fill in the blanks essentially. Then I want to POST that JSON to a web-service.

I'm just not following how to use JSON in LiveCode. I'm able to get JSON, convert to an Array, and get the output, but in this case I'm starting with JSON, and need to keep it as JSON to POST.

I used your function above, and it creates a string that is laid out like proper JSON, but when I POST, it has a 500 error, and I'm guessing it's because it's nto being received as JSON, but as a string.

I don't own the server, so I can't see the server error details.

Re: JSON to String but can't escape quotes sanely?

Posted: Mon Mar 30, 2020 12:59 am
by bangkok
bmcgonag wrote:
Sun Mar 29, 2020 10:59 pm
I used your function above, and it creates a string that is laid out like proper JSON, but when I POST, it has a 500 error, and I'm guessing it's because it's nto being received as JSON, but as a string.
I don't own the server, so I can't see the server error details.
OK that's another issue.

Before POST, you should perhaps create a HTTP header.

Something like :

Code: Select all

put "Accept: application/json, text/javascript, */*; q=0.01"&cr&"Content-Type: application/json; charset=UTF-8" into tHeader
set the httpheaders to tHeader
POST xxx to url tUrl
answer the result
put it into tResult
answer tResult
Last but not least : are your sure your JSON string is ok ? Have you check with :
https://jsonformatter.curiousconcept.com/

Re: JSON to String but can't escape quotes sanely?

Posted: Mon Mar 30, 2020 12:42 pm
by bmcgonag
OK that's another issue.

Before POST, you should perhaps create a HTTP header.

Something like :

Code: Select all

put "Accept: application/json, text/javascript, */*; q=0.01"&cr&"Content-Type: application/json; charset=UTF-8" into tHeader
set the httpheaders to tHeader
POST xxx to url tUrl
answer the result
put it into tResult
answer tResult
Last but not least : are your sure your JSON string is ok ? Have you check with :
https://jsonformatter.curiousconcept.com/
I do have a header for the authorization, but didn't include the content type, so let me set that and give it another shot. Thanks.

Re: JSON to String but can't escape quotes sanely?

Posted: Mon Mar 30, 2020 8:07 pm
by bmcgonag
Okay,

Added content type for application/json and plain/text, which is what I'm passing (along with my Authorization) in postman where it works.

Checked my JSON, and it's valid.

Now, I get 403 Forbidden...so a new thing to figure out. Getting closer. Thanks all.