JSON to String but can't escape quotes sanely?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

JSON to String but can't escape quotes sanely?

Post by bmcgonag » Sun Mar 29, 2020 4:44 pm

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.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

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

Post by Klaus » Sun Mar 29, 2020 5:02 pm

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

bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

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

Post by bmcgonag » Sun Mar 29, 2020 5:15 pm

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.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

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

Post by Klaus » Sun Mar 29, 2020 9:52 pm

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.

bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

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

Post by bmcgonag » Sun Mar 29, 2020 10:59 pm

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.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

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

Post by bangkok » Mon Mar 30, 2020 12:59 am

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/

bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

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

Post by bmcgonag » Mon Mar 30, 2020 12:42 pm

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.

bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

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

Post by bmcgonag » Mon Mar 30, 2020 8:07 pm

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.

Post Reply

Return to “Internet”