1

I am making html tag based player, which needs playlist to play. I have problems reading json file (local file) on chromium on Raspberry Pi 3. Whole thing already works on my laptop with EDGE (getJSON works on Raspberry later in the code). The error I get is statusText: parser error.

I already tried ajax and all combos like: reading text and then parsing it to json object, I played around with ajax parameters (tried jsonp as well). Its funny that the same function works without any problems 10s into the code. Also both json files were checked by validator and are okay.

Btw I have a python script that runs all of that in chromium with --allow-all-files...

//All variables are defined as var globally

//READ PLAYLIST
//This is the function that returns error (it is called by body -> onload)

  function read_playlist() {
    $.getJSON("playlist.json", function (data) {
      music = data;
      cnt = data.length;
    });    
  }


//READ SLIDESHOW
//This is the same function without error (is called by setTimeout(this, 1000))

  $.getJSON("./slideshow.json", function (data1) {
    imgs = data1;
    i_cnt = data1.length;
    i_index = data1.length;
  });

And here is json saved as playlist.json

[{"title":"George Ezra - Paradise","download":"music\/2018 Weekly Charts\/George Ezra - Paradise.mp3","file":"songs\/George Ezra - Paradise.mp3","poster":"images\/logo.png","mp3":"songs\/George Ezra - Paradise.mp3"},{"title":"Jax Jones - Breathe (feat. Ina Wroldsen)_N","download":"music\/2018 Weekly Charts\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3","file":"songs\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3","poster":"images\/logo.png","mp3":"songs\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3"}]

and slideshow.json

["logo.png", "l.jpg"]
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Luka Lah
  • 11
  • 2
  • use `"./playlist.json"` as the path. while that JSON isn't ok, because valid JSON always starts & ends with curly brackets... it might in best case be tolerated. – Martin Zeitler Apr 25 '19 at 08:22
  • You function doesn't return anything. Also it is unclear what `music` and `cnt` are. Are these global variables? – connexo Apr 25 '19 at 08:28
  • 1
    @MartinZeitler *because valid JSON always starts & ends with curly brackets* That is just **wrong**. OP's JSON is perfectly valid. – connexo Apr 25 '19 at 08:29
  • I did tried it with ./ and it seems to make no difference. But that second "json" has no problems (it is just list like object). But thank you anyway :) – Luka Lah Apr 25 '19 at 08:30
  • @connexo that array has no name, which is only tolerated, but not exactly valid syntax (in other environments, this requires relaxing the parser). and since the parser complains about the input, this probably should be pointed out. the term "object notation" hints for, that it isn't an "array notation". – Martin Zeitler Apr 25 '19 at 08:39
  • Possible duplicate of [jQuery returning "parsererror" for ajax request](https://stackoverflow.com/questions/5061310/jquery-returning-parsererror-for-ajax-request) – Martin Zeitler Apr 25 '19 at 08:43
  • @connexo it doesn't matter from which prototype it extends from in JS; see [json.org](https://www.json.org)... it is defined as "a collection of name/value pairs", which suggests the demand for a name property. – Martin Zeitler Apr 25 '19 at 08:48
  • 1
    @MartinZeitler Please note that a file containing any of the following literals would be valid JSON as well: `"foo"`, `true`, `1337`, `""`, `null`. – connexo Apr 25 '19 at 08:48
  • It isn't because I tried everything on that post and still the same problem. Thanks – Luka Lah Apr 25 '19 at 08:48
  • 1
    @MartinZeitler *A JSON value can be an object, array, number, string, true, false, or null.* https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf – connexo Apr 25 '19 at 08:50
  • And also responseText:"whole playlist.json" – Luka Lah Apr 25 '19 at 08:51
  • @MartinZeitler — The description of JSON on json.org is badly written at best. The [current specification](https://tools.ietf.org/html/rfc7159) says "A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array." The JSON in the question is an array so is valid under current and previous specifications. – Quentin Apr 25 '19 at 09:15
  • @Quentin I've tried parsing it in JS and it parses... so the problem might be another. in other environments (eg. `GSON`), one needs to set the parser from "strict" to "lentient" to even parse arrays without a name. – Martin Zeitler Apr 25 '19 at 09:19
  • @MartinZeitler — What parser? It's jQuery that uses JavaScript's JSON.parse under the hood. That doesn't have "strict" and "lentient" modes, and even if it did, *the JSON is valid under the strictest interpretation of any specification you can have*! – Quentin Apr 25 '19 at 09:22
  • @connexo [RFC 4627](https://tools.ietf.org/html/rfc4627) actually defines it... and the strictness of various parser implementations might slightly vary (I'd agree as far as, that in a web context, skipping the names reduces the payload). – Martin Zeitler Apr 25 '19 at 09:28
  • @MartinZeitler — Quoting from that URL: "Obsoleted by: 7159" – Quentin Apr 25 '19 at 09:30
  • I can see all the data in responseText is there any way to read that. I tried data.responseText and it doesn't seem work. – Luka Lah Apr 25 '19 at 09:35
  • @LukaLah if it works on one web-server, but not the other... compare the headers of that one request once in `F12` developer tools, network tab. if it doesn't send `application/json`, the server might not have registered that content-type and might send `text/plain` instead. – Martin Zeitler Apr 25 '19 at 09:35
  • @MartinZeitler I tried to read all of this with ajax with datatype set to text but it didn't work (error just changes to statusText:error). – Luka Lah Apr 25 '19 at 09:40

1 Answers1

0

Okay so the problem was not in the script but in json. I posted json from my laptop which was downloaded from same link that the raspberry has downloaded it. But that python file somehow turns json "hm" to 'hm' so it isn't valid anymore (since 'hm' is not a string).

Luka Lah
  • 11
  • 2