最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my JSON invalid even though it looks correct? - Stack Overflow

programmeradmin5浏览0评论

I've been working on this for quite a while, and I just don't understand why my JSON is invalid...

JSONLint is showing this error

    Error: Parse error on line 107:
...pair?",      "answer": "Yes, as long as the
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

This is the fragment of the JSON

{
    "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, mark as already contacted."
},

{
    "tags": "secured debt",
    "question": "If customer only has secured debts, can we still offer credit repair?",
    "answer": "Yes, as long as they have at least $100 in secured/unsecured debt.
    "},




    {
        "tags": "state",
        "question": "Is the program state sensitive?",
        "answer": "Yes, each partner has particular states that they service. The script engine will only offer services when the state is valid for partner who has that service."
    },

It's failing where it says 'Yes, as long'

JSON is created in ColdFusion dynamically.

<cfscript>faqCounter=1;</cfscript>
    <CFLOOP query="getFAQs">
         <cfoutput>
            {"tags":"#getFAQs.tags#","question":"#getFAQs.question#","answer":"#getFAQs.answer#"}<cfif faqCounter<getFAQCount.getFAQPertinentCount>,</cfif>
         </cfoutput>
        <cfscript>faqCounter++;</cfscript>
    </CFLOOP>

I've been working on this for quite a while, and I just don't understand why my JSON is invalid...

JSONLint is showing this error

    Error: Parse error on line 107:
...pair?",      "answer": "Yes, as long as the
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

This is the fragment of the JSON

{
    "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, mark as already contacted."
},

{
    "tags": "secured debt",
    "question": "If customer only has secured debts, can we still offer credit repair?",
    "answer": "Yes, as long as they have at least $100 in secured/unsecured debt.
    "},




    {
        "tags": "state",
        "question": "Is the program state sensitive?",
        "answer": "Yes, each partner has particular states that they service. The script engine will only offer services when the state is valid for partner who has that service."
    },

It's failing where it says 'Yes, as long'

JSON is created in ColdFusion dynamically.

<cfscript>faqCounter=1;</cfscript>
    <CFLOOP query="getFAQs">
         <cfoutput>
            {"tags":"#getFAQs.tags#","question":"#getFAQs.question#","answer":"#getFAQs.answer#"}<cfif faqCounter<getFAQCount.getFAQPertinentCount>,</cfif>
         </cfoutput>
        <cfscript>faqCounter++;</cfscript>
    </CFLOOP>
Share Improve this question edited Sep 10, 2019 at 22:17 M-- 29.5k10 gold badges70 silver badges106 bronze badges asked Sep 10, 2019 at 19:47 OlegAOlegA 331 silver badge4 bronze badges 17
  • 1 You can't have a literal newline in a JSON string, it should be the escape sequence \n. – Barmar Commented Sep 10, 2019 at 19:50
  • 1 @olegA the \n newline is right before the closing quote here: secured/unsecured debt." – Kyle Commented Sep 10, 2019 at 19:54
  • 2 Don't try to create JSON by filling in each property separately. Use the SerializeJSON() function on the entire array. – Barmar Commented Sep 10, 2019 at 19:54
  • 3 Your code will also fail if any of the variables has double quotes in them, since they won't be escaped. – Barmar Commented Sep 10, 2019 at 19:55
  • 1 Don't try to patch it in the database. Your whole approach is wrong. Do what I said above. – Barmar Commented Sep 10, 2019 at 19:57
 |  Show 12 more ments

3 Answers 3

Reset to default 4

you have a CRLF inside the the quotes ""

"answer": "Yes, as long as they have at least $100 in secured/unsecured debt.
"},

( 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 remended. 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".

The issue is that the string contains a newline as a literal, which should be \n. In most languages you are able to filter or serialize data into JSON and it will handle these conversions for you.

Consider the following code snippets from https://helpx.adobe./coldfusion/cfml-reference/coldfusion-functions/functions-s/serializejson.html

this script utilizing the serializeJSON() function converts data into JSON

<cfscript>
       example = structnew();
       example.firstname = "Yes";
       example.lastname = "Man";
       // changing the default serialization by specifying the type of "firstname" as string
       metadata = {firstname: {type:"string"}};
       example.setMetadata(metadata);
       writeoutput(SerializeJSON(example));
</cfscript>
{"LASTNAME":"Man","FIRSTNAME":"Yes"}
发布评论

评论列表(0)

  1. 暂无评论