2

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"');
3
  • 1
    Essentially what you're doing is unconvert a string in extras.custom part of a JSON into a JSON sub-object. This means the JSON parser fails at recognizing escaped strings. Are you perchance using skyboy's JSON parser? Maybe you should try a different JSON parser in AS3. Commented Aug 7, 2017 at 7:27
  • @Vesper, yes I think that the parser is trying to parse the string custom. I am using the parser to Air 26. So I guess the question is why does the adobe parser not recognize the inner object as a string. Commented Aug 7, 2017 at 10:50
  • I think try an alternate parser, say this one github.com/blooddy/blooddy_crypto and check if it'll work. If yes, send a bug report to adobe, if no, really review the internals of received data. Maybe there is a quotes imbalance somewhere inside. Commented Aug 7, 2017 at 11:30

3 Answers 3

2
+100

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Great, I now understand.
2

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"},

Comments

1

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\"}")

2 Comments

I do not think this works because my json data is stored as a string, and so it cant be assigned to an object, as I just get an object containing the string. Hence using JSON.parser.
This question is for Actionscript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.