[Resolved] JSON.parse issue

Posted on
  • I use the following code on a desktop computer as well as on an ESP8266 (2v04).

    function getTime() {
    
            require("http").get("http://worldtimeapi­.org/api/timezone/America/Toronto", function (res) {
                    res.on('data', function (data) {
                            console.log(data);
                            console.log(JSON.parse(data).datetime);
    
                    });
                    res.on('close', function (data) {
                            console.log("Connection closed");
                    });
            });
    }
    
    getTime();
    

    node.js on the desktop could parse the output just fine :

    <Buffer 7b 22 77 65 65 6b 5f 6e 75 6d 62 65 72 22 3a 35 30 2c 22 75 74 63 5f 6f 66 66 73 65 74 22 3a 22 2d 30 35 3a 30 30 22 2c 22 75 74 63 5f 64 61 74 65 74 ... >
    2019-12-15T12:50:08.336992-05:00
    

    ESP8266 shows the following:


    1 Attachment

    • Screen Shot 2019-12-15 at 1.00.35 PM.png
  • Can you please post the JSON in its entirety?

  • This is JSON as parsed by node.js on a desktop:

    { week_number: 50,
      utc_offset: '-05:00',
      utc_datetime: '2019-12-15T18:10:33.811491+00:00',
      unixtime: 1576433433,
      timezone: 'America/Toronto',
      raw_offset: -18000,
      dst_until: null,
      dst_offset: 0,
      dst_from: null,
      dst: false,
      day_of_year: 349,
      day_of_week: 0,
      datetime: '2019-12-15T13:10:33.811491-05:00',
      client_ip: '174.114.161.151',
      abbreviation: 'EST' }
    

    The actual text string comes from https://worldtimeapi.org/api/timezone/Am­erica/New_York

  • res.on('data', is triggered as soon as the first data arrives, not when all data is there. That causes the error, if you replace the line console.log(JSON.parse(data).datetime); with console.log('###'); you'll see that `res.on('data', is triggered multiple times.
    As such you're trying to parse an incomplete JSON string, and the error is normal.
    Edit: And it's pure luck that it works in node.js.

  • If you know the protocol between JSON strings - such as cr/lf, cr, lf, you can write something like is done for GPS where the (non-JSON) sentences come in line by line. With every new arrival of a data fragment you look for the separation (ends and begins) and process when one found... see GPS module source at https://www.espruino.com/modules/GPS.js

  • Check this snippet to set the time:

    http://forum.espruino.com/conversations/­280894/#12755183

    Find more at the "Tips and Tricks" page

  • Just to add the fix for what you want is just the second example here: https://www.espruino.com/Internet#client­

    require("http").get("http://www.espruino­.com", function(res) {
      var contents = "";
      res.on('data', function(data) { contents += data; });
      res.on('close', function() { console.log(contents); });
    });
    

    basically get all the data, then parse in one go when the socket is closed

  • Thanks.

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

[Resolved] JSON.parse issue

Posted by Avatar for user106712 @user106712

Actions