micahhoover
micahhoover

Reputation: 2160

JSON unexpected token chrome

My browser (chrome) doesn't seem to like my JSON (formatted via ASP.NET's return Json(object)).

My JavaScript handler where it fails looks like this:

var responseObj = JSON.parse(msg);

Before I parse it I can see all the contents in the chrome JavaScript debugger, but it fails when I send it to JSON.parse().

The truly wierd thing is JSONLint said it was valid.

My JSON looks like this (actual object is much longer believe it or not):

{
    "crct": {
        "ETL_ID": 221,
        "DELETE_FLAG": "N",
        "ID": "a0Wc00000008sZGEAY",
        "ISDELETED": "false",
        "NAMEX": "CRCT-4474",
        "CREATEDDATE": "/Date(1345769442000)/",
        "CREATEDBYID": "005C00000049nuYIAQ",
        "LASTMODIFIEDDATE": "/Date(1345771729000)/",
        "LASTMODIFIEDBYID": "005C00000049nuYIAQ",
        "SYSTEMMODSTAMP": "/Date(1345771729000)/",
        "CLIENT_TASK": "a0Xc0000001lLihEAE",
        "COUNTRY": null,
        "PRA_CRCT_ID": null,
        "PROJECT_REGION": "a0mc00000008ZPgAAM",
        "TOTAL_UNITS_FRM": 200,
        "UNIT_SCOPE_CHANGE": null,
        "UNIT_SCOPE_DIFFERENCE": 0,
        "CUMULATIVE_OFFICIAL_FORECAST_U": 200,
        "CUMULATIVE_PENDING_DOWNSCOPE_U": 0,
        "CUMULATIVE_BASELINE_UNITS_ROLL": 200,
        "CUMULATIVE_WORKED_HOURS_ROLLUP": 0,
        "CUMULATIVE_WORKED_UNITS_ROLLUP": 0,
        "CLIENT_UNIT_NUMBER": null,
        "EntityState": 2,
        "EntityKey": {
            "EntitySetName": "SF_CLIENT_REGION_COUNTRY_TASK",
            "EntityContainerName": "SFEntities",
            "EntityKeyValues": [
                {
                    "Key": "ETL_ID",
                    "Value": 221
                }
            ],
            "IsTemporary": false
        }
    },
    "unitChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "effortChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "hourChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    },
    "effortRatioChecks": {
        "gaps": false,
        "duplicates": false,
        "startDate": "/Date(1343793600000)/",
        "endDate": "/Date(1370059200000)/",
        "liDurations": [
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Baseline",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Previous Official Forecast",
                "gaps": false,
                "duplicates": false
            },
            {
                "thisTypeFound": true,
                "startDate": "/Date(1343793600000)/",
                "endDate": "/Date(-62135578800000)/",
                "type": "Historical",
                "gaps": false,
                "duplicates": false
            }
        ],
        "numberOfUniqueBufCodes": 0
    }
}

Upvotes: 2

Views: 3546

Answers (4)

Zen
Zen

Reputation: 17

I have run into this problem (with Chrome) with a simple JSON attempt:

var TOC = { id: 1, title: "header" }

$.parseJSON gave the same error.

The workaround I found is to enclose it in an array (and then refer to index zero).

var TOC = [{ id: 1, title: "header" }][0]

Upvotes: 0

Shanimal
Shanimal

Reputation: 11718

I got this same error when building an app with fusion tables. The responses from google were not being parsed correcctly. I replaced the native json library with json2.js and the problem was solved.

https://github.com/douglascrockford/JSON-js

Upvotes: 1

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Without seeing your code, it is hard to say what is happening, but your JSON is fine:

Now, if you are trying to use this in Javascript Coding then you shouldnt' be using JSON, but should just be assigning an object. No parsing is needing, the compiler does that for you.

in other words -- don't do what I just did. I'm just using it to show that as JSON it would parse correctly under Chrome (21.0.1180.57)

<html>
<head>
<title>Quick test</title>

<script>
var a = '\
{\
    "crct": {\
        "ETL_ID": 221,\
        "DELETE_FLAG": "N",\
        "ID": "a0Wc00000008sZGEAY",\
        "ISDELETED": "false",\
        "NAMEX": "CRCT-4474",\
        "CREATEDDATE": "/Date(1345769442000)/",\
        "CREATEDBYID": "005C00000049nuYIAQ",\
        "LASTMODIFIEDDATE": "/Date(1345771729000)/",\
        "LASTMODIFIEDBYID": "005C00000049nuYIAQ",\
        "SYSTEMMODSTAMP": "/Date(1345771729000)/",\
        "CLIENT_TASK": "a0Xc0000001lLihEAE",\
        "COUNTRY": null,\
        "PRA_CRCT_ID": null,\
        "PROJECT_REGION": "a0mc00000008ZPgAAM",\
        "TOTAL_UNITS_FRM": 200,\
        "UNIT_SCOPE_CHANGE": null,\
        "UNIT_SCOPE_DIFFERENCE": 0,\
        "CUMULATIVE_OFFICIAL_FORECAST_U": 200,\
        "CUMULATIVE_PENDING_DOWNSCOPE_U": 0,\
        "CUMULATIVE_BASELINE_UNITS_ROLL": 200,\
        "CUMULATIVE_WORKED_HOURS_ROLLUP": 0,\
        "CUMULATIVE_WORKED_UNITS_ROLLUP": 0,\
        "CLIENT_UNIT_NUMBER": null,\
        "EntityState": 2,\
        "EntityKey": {\
            "EntitySetName": "SF_CLIENT_REGION_COUNTRY_TASK",\
            "EntityContainerName": "SFEntities",\
            "EntityKeyValues": [\
                {\
                    "Key": "ETL_ID",\
                    "Value": 221\
                }\
            ],\
            "IsTemporary": false\
        }\
    },\
    "unitChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "effortChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "hourChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    },\
    "effortRatioChecks": {\
        "gaps": false,\
        "duplicates": false,\
        "startDate": "/Date(1343793600000)/",\
        "endDate": "/Date(1370059200000)/",\
        "liDurations": [\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Baseline",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Previous Official Forecast",\
                "gaps": false,\
                "duplicates": false\
            },\
            {\
                "thisTypeFound": true,\
                "startDate": "/Date(1343793600000)/",\
                "endDate": "/Date(-62135578800000)/",\
                "type": "Historical",\
                "gaps": false,\
                "duplicates": false\
            }\
        ],\
        "numberOfUniqueBufCodes": 0\
    }\
}';

var b = JSON.parse(a);
window.alert(typeof b);
window.alert(JSON.stringify(b));

</script>
<body>
This is a test
</body>
</html>

Upvotes: 1

Dancrumb
Dancrumb

Reputation: 27539

JSON.parse is for parsing a JSON string into a JavaScript object.

If you try and parse an object (which is what you've posted in your question), you will get an 'Unexpected token' message in Chrome.

So, either your trying to convert something that doesn't need converting, or you want to turn an object into JSON, in which case you need stringify

Upvotes: 3

Related Questions