Which characters are valid/invalid in a JSON key name?
No. Any valid string is a valid key. It can even have "
as long as you escape it:
{"The \"meaning\" of life":42}
There is perhaps a chance you'll encounter difficulties loading such values into some languages, which try to associate keys with object field names. I don't know of any such cases, however.
Why are there invalid characters in this JSON?
{
"Master": {
"Major": "S",
"Minor": "E",
"IPAddress": "0.0.0.0",
"Detail": "<root><key keyname=\"state\">3</key><key keyname=\"oldState">1</key><key keyname=\"currency\"></key><key keyname=\"denomination\"></key></root>",
"SourceCreateDate": "2014-04-03T14:02:57.182+0200"
},
"Messages": [
{
"MessageCode": "0",
"MessageType": "8"
}
]
}
JSON validator: http://jsonlint.com/
Edit: Explication: when you open a "
you need to close it on the same line. So you have to put your xml on a single line or to escape it.
Why is my JSON invalid even though it looks correct?
( As the other answers already pointed out, the problem is the un-escaped new line, which breaks the JSON. That's one of the reasons to avoid DIY JSON. Instead, use the built in function SerializeJSON(). )
Lucee 5.2.8.39+
Try the new support for JSON serialization-related settings in the Application.cfc. The new settings let you override the bizarre default CF has for serializing query objects:
// serialize queries as an array of structures AND
// preserves the column name case used in the sql
this.serialization.preserveCaseForStructKey = true;
this.serialization.serializeQueryAs = "struct";
Now you can skip all the query looping. Simply execute the query and call serializeJSON( yourQuery )
, to generate a wonderfully sane looking string like this:
[
{
"answer": "Yes, mark as already contacted.",
"tags": "already transferred",
"question": "Can we transfer customers who have already been transferred previously? what is the dispo? warm transfer or already contacted?"
},
{
"answer": "Yes, as long as they have at least $100 in secured/unsecured debt. ",
"tags": "secured debt",
"question": "If customer only has secured debts, can we still offer credit repair?"
}
]
Earlier Lucee versions
For earlier versions, do what @Barmar recommended. Build an array of structures. Then use serializeJSON to convert the array into a properly formatted JSON string.
Runnable Example
<cfset yourArray = []>
<cfloop query="getFAQs">
<cfset yourArray.append( { "tags" : getFAQs.tags
, "question" : getFAQs.question
, "answer": getFAQs.answer
} )>
</cfloop>
<cfset jsonString = serializeJSON( yourArray )>
How to remove the new line?
After generating a "proper" JSON string, run a replace() and substitute \n
with an empty string.
<cfset jsonString = replace(jsonString , "\n", "", "all")>
To permanently remove them, you'll have to find the code inserting them into the database in the first place, and modify it there. Also, update any existing database records to remove the "\n".
Related Topics
Does JavaScript Support 64-Bit Integers
What the Difference of This.State and This.Setstate in Reactjs
How to Check If Page Exists Using JavaScript
Download PDF File Using Jquery Ajax
How to Define Custom Sort Function in JavaScript
How to Remove One Specific Selected File from Input File Control
Using the Get Parameter of a Url in JavaScript
JavaScript Closures and 'This'
Why Array.Indexof Doesn't Find Identical Looking Objects
Reading Client Side Text File Using JavaScript
Method Overloading in JavaScript
How to Loop Through All the Elements Returned from Getelementsbytagname
Hide Select Option in Ie Using Jquery
Migrating Angularjs to Angular 4,5 (With Demo)
Console.Log Showing Only the Updated Version of the Object Printed