Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Parsing Error

I got problem. I have this JSON automatically generated by Open Flash Chart php library. The problem is, OFC report JSON Parse Error [Syntax Error] while test result using http://www.jsonlint.com/ report that my JSON is fine. But, w3c parser report error too:(

Any help?

Here's the JSON:

{
    "title": "Followers Trend",
    "elements": [
        {
            "type": "area_hollow",
            "fill-alpha": 0.35,
            "values": [

            ],
            "colour": "#5B56B6",
            "text": "Followers",
            "font-size": 12 
        } 
    ],
    "x_axis": {
        "colour": "#A2ACBA",
        "grid-colour": "#D7E4A3",
        "offset": false,
        "steps": 4,
        "labels": {
            "steps": 2,
            "rotate": "vertical",
            "colour": "#A2ACBA",
            "labels": [

            ] 
        } 
    },
    "x_legend": {
        "text": "Week Trend (2009-08-17 - 2009-08-24)",
        "style": "{font-size: 20px; color: #778877}" 
    },
    "y_axis": {
        "min": 0,
        "max": 150,
        "steps": 30 
    }
}
like image 834
ariefbayu Avatar asked Aug 24 '09 10:08

ariefbayu


People also ask

What is parse JSON error?

When it detects invalid JSON, it throws a JSON Parse error. For example, one of the most common typos or syntax errors in JSON is adding an extra comma separator at the end of an array or object value set. Notice in the first few examples above, we only use a comma to literally separate values from one another.

How do you handle JSON parsing errors?

The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

How do I fix JSON format error?

Follow these steps to resolve your invalid JSON formatting error. Verify that the service account credentials are in a valid JSON format. Tip: An online JSON formatter can identify problems with the JSON format. If the error persists, generate a new service account credentials key.

What is JSON parsing example?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.


1 Answers

A few things I learned while playing with JSON is:

  • If you have validate the JSON on various JSON validation services and the result is GOOD. But, when you failed to eval it, try to wrap your JSON using ( and ) => ({jsondata})

    var json = eval( "(" + jsonString + ")" );
    
  • NEVER build the JSON yourself. It's a gate to failure. Always use official or popular JSON library (depending on your language). For example:

    • On PHP: use json_encode()
    • On Java Android: use org.json.JSONObject
    • A list of all other available library to play with JSON is listed in JSON official page.
  • To display and format JSON data, you can use JSONViewer.
like image 64
ariefbayu Avatar answered Oct 12 '22 15:10

ariefbayu