0

I am using JSON.stringify to stringify an object. The resultant string is like this

'{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"150\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"37\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"71\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"85\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"134\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}]","startIndex":"0","totalObjects":"6"}'

Now When I am trying to parse it again into object using JSON.parse(). It is giving me an error "Uncaught SyntaxError: Unexpected token @". I see there are keys that are starting with @ but I am not able get the error or find any fix.

Please Help!

2
  • 3
    Please post how you stringify the object and when/how you are trying to parse the JSON again. Looks like you have a problem with escaping the " properly. A better solution would be to not have JSON contain other JSON (objects should be encoded an array, not a string containing JSON). Commented Jun 4, 2014 at 4:55
  • For some reason I had to get an object from some webapp and hardcode it somewhere else to test some functionality. For that I am calling JSON.stringify on developers console and using this string. Commented Jun 4, 2014 at 5:00

3 Answers 3

1

The resultant string is like this

That seems to be only what you get as an output, e.g. from console.log. It is not a string literal represenation of the string you have, but rather "'" + jsonstring + "'".

If you have used that output as a string literal, and got the parse error from that, you'd rather need to use

'{"@type":"page","count":"6","endIndex":"0","objects":"[{\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"92\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"150\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"37\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"71\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"85\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"134\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}]","startIndex":"0","totalObjects":"6"}'

Of course, as @FelixKling mentioned in the comments, having JSON strings inside JSON is an antipattern (just as is having JSON in string literals).

> var obj = {"@type":"page","count":"6","endIndex":"0","startIndex":"0","totalObjects":"6"};
[Object]
> var objects = [{"@type": "viewableObject", "name": "Body1", "objectDbId": "92", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "150", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "37", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "71", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "85", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "134", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}]
[Object]
> obj.objects = JSON.stringify(objects); // first mistake
'[{"@type": "viewableObject", "name": "Body1", "objectDbId": "92", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "150", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "37", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "71", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "85", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}, {"@type": "viewableObject", "name": "Body1", "objectDbId": "134", "objectId": "allViews", "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a", "type": "view"}]'
> JSON.stringify(obj) // the output you got:
'{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"150\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"37\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"71\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"85\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"134\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}]","startIndex":"0","totalObjects":"6"}'
> copied and used as a string literal:
> JSON.parse('{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"150\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"37\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"71\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"85\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"134\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}]","startIndex":"0","totalObjects":"6"}')
Parse Error
> // instead, use the string itself:
> var str = JSON.stringify(obj);
> JSON.parse(str)
[Object]
Sign up to request clarification or add additional context in comments.

Comments

0
{\"@type

this is your issue I think....an unwanted escaping of "

2 Comments

So if I remove this unwanted escaping. would it work?
@KingJames: Instead of "unwanted escaping", I'd suspect "uncomplete escaping"
0

What is your original JSON object, because your stringify is returning wrong json-string.

I am assuming your original json is

{
    "@type": "page",
    "count": "6",
    "endIndex": "0",
    "objects": [
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "92",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "150",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "37",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "71",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "85",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "134",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        }
    ]
}

and using JSON.stringify returns

"{"@type":"page","count":"6","endIndex":"0","objects":[{"@type":"viewableObject","name":"Body1","objectDbId":"92","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"150","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"37","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"71","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"85","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"134","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"}]}"

can you post your original json?

7 Comments

My original JSON is the same you wrote.
Then what are you using to stringify it? coz on my side it gives me correct string.
I am using JSON.stringify. Are you saying that the unwanted escaping is the incorrect thing I am getting?
Apparently yes. its javascript JSON.stringify right?
Yes it is. It is always giving me escape characters. Is it because I am doing it in developers console?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.