Jump to content

Escaping quotes in JSON data


naio

This topic is 1124 days old. Please don't post here. Open a new topic instead.

Recommended Posts

 The content of myField is:

text before quotes "quoted text"

and I want to format a JSON object:

JSONFormatElements ( "{\"object\":\"" & myTable::myField & "\"}")

the quotes within my data make invalid JSON, so I escape quotes:

JSONFormatElements ( "{\"object\":\"" & Substitute ( myTable::myField; "\""; "\\\"")  & "\"}")


Then I get valid JSON like this:

{"object" : "text before quotes \"\u0016quoted text\u0016\""}

I wonder where these \u0016 characters added around the quoted text come from, I don't want them there

How should I escape quotes without this problem?

Edited by naio
Link to comment
Share on other sites

20 minutes ago, naio said:

where these \u0016 characters added around the quoted text come from

Apparently your field contains some invisible control characters . You can test this by checking the result of:

Code ( myTable::myField )

for the presence of the string "00022".

---
BTW, is there a reason you won't let Filemaker create the JSON object for you by using:

JSONSetElement ( "" ; "object" ; myTable::myField ; 1 )

and save yourself the trouble of escaping the quotes (and other special characters)?

Link to comment
Share on other sites

Oops! :blush3: you are right, actually those characters are in the source data but invisible within FM.

Regarding the use of JSONSetElement: I think that, for large JSON objects, JSONFormatElements avoids the nesting of several JSONSetElement functions. Anyway I'll consider your advice.

Thanks for your help!

 

Link to comment
Share on other sites

7 minutes ago, naio said:

JSONFormatElements avoids the nesting of several JSONSetElement functions.

I don't think so. JSONFormatElements has nothing to do with creating JSON. All it does is look at a string, and if the string is a valid JSON string, it formats it. It doesn't care how the string was created.

 

Link to comment
Share on other sites

You're absolutely right, I'm discovering the world of JSONSetElement with brackets, already got rid of that JSONFormatElement...

Thank you,

Link to comment
Share on other sites

This topic is 1124 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.