Mar
Mar

Reputation: 323

Actionscript: Invalid JSON parse input for what seems valid

I have the following JSON

{
    "extras": {
        "google.sent_time": 1502027522898,
        "custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
        "from": "62572096498",
        "alert": "Read More...",
        "title": "New message",
        "google.message_id": "0:2905559%2ecccafd7ecd"
     } 
}

Using

var jsonObj:Object = JSON.parse(str);

Gives the error:

SyntaxError: Error #1132: Invalid JSON parse input.
    at JSON$/parseCore()
    at JSON$/parse()

I do not understand why this is, the JSON is valid.

Additional information,

The solution I have tried and works is as follows, despite the before and after been valid.

var clean:String = str.split("\\").join('');
clean = clean.replace('"custom":"{"a"', '"custom":{"a"');
clean = clean.replace('"}","from"', '"},"from"');

Upvotes: 2

Views: 2406

Answers (3)

cameraman
cameraman

Reputation: 252

If this "JSON" is part of your actionscript it's an Object, not a JSON. The JSON.parse method won't work because accepts JSON-formatted String as first parameter and you pass and Object instead.
If you load/import this script from a JSON file, the JSON.parse method will work.

// importing the external JSON file
function loadJSON() {
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, decodeJSON);
    loader.load(new URLRequest("test.json"));
}

// converting to actionscript Object
function decodeJSON(e:Event):void {
    var loader:URLLoader = URLLoader(e.target) ;
    var jsonObj:Object = JSON.parse(loader.data);
    trace(jsonObj["extras"]["custom"]["a"]["message_id"]);
}

loadJSON();

If you want to access the "custom" value, uncomment double quotes in the JSON file:

  "custom": {"a":{"message_data":"","message_id":"749","message_command":"MESSAGE"},"i":"899ec3dd"},

Upvotes: 2

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27192

Few observations :

  • The JSON provide in OP is looking like a JSON object instead of JSON string.Hence, No need to parse the whole object.
  • As partialJsonObj.extras.custom is a JSON string parse it to convert into JSON Object.

DEMO

var partialJsonObj = {
    "extras": {
        "google.sent_time": 1502027522898,
        "custom": "{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}",
        "from": "62572096498",
        "alert": "Read More...",
        "title": "New message",
        "google.message_id": "0:2905559%2ecccafd7ecd"
     } 
};

partialJsonObj.extras.custom = JSON.parse(partialJsonObj.extras.custom);

var jsonObj:Object = partialJsonObj;

console.log(jsonObj);

Upvotes: 2

Artem
Artem

Reputation: 2314

I believe str is a javascript object already, so nothing to parse and you can simply assign it like:

var jsonObj:Object = str;

However I'd assume you need to parse and convert to object your custom property:

a.extras.custom = JSON.parse("{\"a\":{\"message_data\":\"\",\"message_id\":\"749\",\"message_command\":\"MESSAGE\"},\"i\":\"899ec3dd\"}")

Upvotes: 1

Related Questions