• JSON

    var json = {a:1,b:2};   
    var data = JSON.stringify(json);  // '{"a":1,"b":2}'
    

    JSON5

    var json = {a:1,b:2};
    var data = JSON5.stringify(s);     // "'{a:1,b:2}'"
    

    Possible workaround to convert standard JSON to JSON5 for .stringify()

    var json = {a:1,b:2}; 
    var data = JSON.stringify(json).replace(/\"/g, '');  // '{a:1,b:2}'
    

    But how to convert a JSON5 data string back to a json object in Espruino?

  • Try E.toJS? This is basically JSON5, but I think things like pin names and functions can also be dumped.

    For parsing, you can just use eval - but obviously you need to be able to trust the data since there are security issues there (any JS code in the input will get executed!).

  • first shot

    var json = {a:1,b:2}; 
    var data = JSON.stringify(json).replace(/\"/g, '');  // '{a:1,b:2}'
    var j = E.toJS(data)     // "\"{a:1,b:2}\""
    

    Ok, no error but no object - whats missing ?

  • that .replace won't work with string values like {a:"Hello",b:2};

    as for converting - quick and dirty may be eval('('+data+')')

  • Yes, text in json will not be covered ....

    ok so let's sum it up

    var json = {a:"1",b:2};
    data = E.toJS(json);    // {a:\"1\",b:2}"
    j = eval('('+data+')');  // {  a: "1", b: 2 }
    
    

    So, JSON5 is possible on Espruino devices - thanks.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

What about JSON5 style for JSON5.stringify() in Espruino?

Posted by Avatar for MaBe @MaBe

Actions