-2

I'm trying to parse some JSON, but it seems I have bad control characters, even though http://jsonlint.com indicates it's valid. What would I need to change for it to be actually valid?

{"panes": [{"col": {"3": 1}, "row": {"3": 1}, "width": 1, "widgets": [{"type": "Slider", "settings": {"max": 100, "min": 0, "step": 1, "color": "grey", "onSlide": "// Example: Convert temp from C to F and truncate to 2 decimal places.\n// return (datasources[\"MyDatasource\"].sensor.tempInF * 1.8 + 32).toFixed(2);\n\n", "showvalue": 1, "initialvalue": "0"}}], "col_width": 1}], "columns": null, "plugins": [], "version": 1, "allow_edit": true, "datasources": []}

This is the output form JSONLint, which indicates its Valid JSON. When trying to parse it using JSON.parse(), I get the folling error:

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 235 of the JSON data

{
    "panes": [{
        "col": {
            "3": 1
        },
        "row": {
            "3": 1
        },
        "width": 1,
        "widgets": [{
            "type": "Slider",
            "settings": {
                "max": 100,
                "min": 0,
                "step": 1,
                "color": "grey",
                "onSlide": "// Example: Convert temp from C to F and truncate to 2 decimal places.\n// return (datasources[\"MyDatasource\"].sensor.tempInF * 1.8 + 32).toFixed(2);\n\n",
                "showvalue": 1,
                "initialvalue": "0"
            }
        }],
        "col_width": 1
    }],
    "columns": null,
    "plugins": [],
    "version": 1,
    "allow_edit": true,
    "datasources": []
}

I was trying to parse a php object property.

var js_object = JSON.parse('<?= php_object->json ?>');
waspinator
  • 6,464
  • 11
  • 52
  • 78
  • https://en.wikipedia.org/wiki/Byte_order_mark – diavolic Apr 17 '17 at 03:44
  • 1
    Its already a JS object you need to parse it – Shubham Khatri Apr 17 '17 at 03:49
  • 1
    I can only guess that you put that exact value into a string literal (`'...'`) and tried to parse that value. That won't work since the escape characters are not properly escaped. So, either [escape the escape characters](https://jsfiddle.net/4p1zvzrs/) or better, don't put it in a string literal and don't use `JSON.parse`, let JavaScript evaluate it as an object literal. – Felix Kling Apr 17 '17 at 03:50
  • Thanks @FelixKling, that's what it was. I'm confused now at the purpose of `JSON.parse`. I see a lot of examples like `JSON.parse('{"foo" : 1 }');` but it seems that it's unnecessary as `{"foo" : 1 }` is already a js object. – waspinator Apr 17 '17 at 13:28
  • 1
    That's correct. But not every string value is created via a string literal. You could store JSON encoded data in local storage, a cookie, receive it from a web worker or via an XHR request. – Felix Kling Apr 17 '17 at 14:05
  • 1
    The actual issue here seems to be [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/q/23740548/218196) – Felix Kling Apr 17 '17 at 14:06

1 Answers1

1

There is not need for you to parse the data, you can directly access it and every object using the Javascript dot or [brackets] notation

var data = {
    "panes": [{
        "col": {
            "3": 1
        },
        "row": {
            "3": 1
        },
        "width": 1,
        "widgets": [{
            "type": "Slider",
            "settings": {
                "max": 100,
                "min": 0,
                "step": 1,
                "color": "grey",
                "onSlide": "// Example: Convert temp from C to F and truncate to 2 decimal places.\n// return (datasources[\"MyDatasource\"].sensor.tempInF * 1.8 + 32).toFixed(2);\n\n",
                "showvalue": 1,
                "initialvalue": "0"
            }
        }],
        "col_width": 1
    }],
    "columns": null,
    "plugins": [],
    "version": 1,
    "allow_edit": true,
    "datasources": []
}

console.log(data.panes);
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • What if the data is retrieved via an XHR request? What makes you think the data is in the JS source? – Felix Kling Apr 17 '17 at 03:52
  • It's possible the server returned the response with appropriate headers, so there's no need to parse it again – gaganshera Apr 17 '17 at 03:53
  • @gaganshera: Server responses are not parsed automatically. You might be thinking of jQuery. Also, if the data was already parsed and the OP tried to pass an object to `JSON.parse`, the error would be different. – Felix Kling Apr 17 '17 at 03:55
  • Yes I'm assuming that the ajax function already parsed the response – gaganshera Apr 17 '17 at 03:56
  • I was getting the json from a php variable. But I thought I couldn't just use a string as a javascript object. I was trying to `var js_object = JSON.parse('=php_object->json');`. This seems to work instead: `var js_object = =php_object->json?>`. – waspinator Apr 17 '17 at 13:12
  • That's what I had assumed when I wrote the answer. Glad it helped – Shubham Khatri Apr 17 '17 at 13:13