0

In javascript, I need to parse a string into what would be the equivalent of a javascript array such as:

[["2016-12-21",101.58],["2016-12-22",209.56]]

The data I have, along with my attempt at parsing it and the error message looks like this:

str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]"

JSON.parse(str)

Uncaught SyntaxError: Unexpected token & in JSON at position 2(…)

I attempted to remove some of the special characters from the string (&, #, ;, etc.), which slightly changed the error message, but I was never able to get to my end result. I figured I would put it in stackoverflow starting from the beginning, because there is most likely a better start-to-finish solution than what I was attempting.

5
  • ur data in str has the double quotes as html entities (" vs "), and that is not valid json. The array at the top of your question has the quotes correct, so some other process is mucking up your string Commented Nov 19, 2016 at 2:13
  • str is not valid json when it uses html entities as quotes. Fix the source. Use jsonlint.com to validate your json Commented Nov 19, 2016 at 2:14
  • Are you getting this data from browser View source window? Commented Nov 19, 2016 at 2:20
  • 1
    How are you getting the data into the string in the first place? It's usually not a good idea to put JSON data into a string literal programmatically. Commented Nov 19, 2016 at 2:25
  • See comment below (data comes from python, specifically a csv file read with pandas). Commented Nov 19, 2016 at 2:45

4 Answers 4

2

Naively,

JSON.parse(str.replace(/"/g,'"'));

will work, but you should figure out why your double quotes are getting turned into HTML entities.

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

3 Comments

This worked. My data originates from using Python on the server side, and using the pandas library. The call looks like this: str = stock_data.to_json(orient='values') Where stock_data was read in from csv using read_csv.
I don't know python, but a method called to_json seems like its not working properly if it does not return valid JSON
I am using Flask (which uses Jinja). I think the json properties get lost when I send the JSON object (from python) to the html template, which has inline javascript. (I called it like '{{ str }}')
0

Description

This will allow for the numerical ascii character of any character. However, if you only have quotations to remove it isn't too much to run a replace.

var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]";
// regex to get the ascii number
var r = /\&#(\d\d);/gi;
// replaces each numerical ascii character with their character representation
str = str.replace(r, function (match, grp) {
    return String.fromCharCode(parseInt(grp)); } );

console.log(JSON.parse(str));

Comments

0

Try to decode your encoded string reusing the HTML parser in something more portable, a lot faster, reusable. regex patterns are specific to the current problem and each time you encounter e different character you'll have write and rewrite a new pattern. I use an encapsulated function method similar to this:

function htmlParse( x ) {
     var c = document.createElement("div");
         c.innerHTML = x;
  return c.innerText;       
 };

Now let's decode your retrieved string...

var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]";

|>>

var decoded = htmlParse( str );
console.log( decoded );

and see if parsing with json succeeds

var arr = JSON.parse( decoded );
console.log( arr );

and here is the snippet...

    var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]";


    var decoded = htmlParse(str);
    console.log(decoded);

    var arr = JSON.parse(decoded);
    console.log(arr);

    function htmlParse(x) {
      var c = document.createElement("div");
      c.innerHTML = x;
      return c.innerText;
    };

Comments

0

If using the json package to write the data, set the ensure_ascii argument to false.

with open('output.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False)

For Python2.x, use the codecs library to set the encoding of your output file when you open it.

Comments

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.