locked
Invalid Character error from JSON.parse() when string read from a file RRS feed

  • Question

  • Metro style HTML & javascript app

    I created a folder named 'data' at the root of my project. I added a javascript file named x.js using Add | New Item from the Solution Explorer. Within that file I have the following:

    [
    ]

    (yes, there is a CRLF between the two brackets)

    I use something similar to the following to read and parse the file as json:

    Windows.Storage.PathIO.readTextAsync("ms-appx:///data/conference.js", Windows.Storage.Streams.UnicodeEncoding.utf8)
    .then(function(s){var o = JSON.parse(s);});

    This throws an Invalid Character exception from the JSON.parse(). If I .trim() the string before parsing then there is no error:

    var o = JSON.parse(s.trim());

    Could PathIO.readTextAsync() be returning the Byte Order Mark?

    TIA

    -Mike

    Wednesday, May 16, 2012 1:52 AM

Answers

  • PS,

    It is in conjunction with the BOM.  You can work around this by opening the file and removing the BOM.

    1. Right click on your file
    2. Open with
    3. Choose: Souce Code (Text) Editor With Encoding
    4. For encoding choose: US-ASCII - COdepage 20127

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 17, 2012 4:16 PM
    Moderator

All replies

  • Mike,

    The issue is the CR/LF and that there is no JSON data inside there.  Doing the trim removes the LF.

    That said, we should probably handle this better.  I filed a bug on this.

    Thanks for finding this!

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 17, 2012 4:02 PM
    Moderator
  • PS,

    It is in conjunction with the BOM.  You can work around this by opening the file and removing the BOM.

    1. Right click on your file
    2. Open with
    3. Choose: Souce Code (Text) Editor With Encoding
    4. For encoding choose: US-ASCII - COdepage 20127

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 17, 2012 4:16 PM
    Moderator