JSONs files

edited January 2016 in p5.js

I need to read (and write in the future) a list of names and infos that will congregate into many array of objects, and to make it easyer to store and organize this infos I would like to save it to a JSON file;The problem is that using P5.js, every attempt to read such files results in a "Array[ ]" response from the browser consolle. I tryed to debug the example page at p5.js reference and it gives the same result, an empty array. Trying to load a local file gives this errors: "syntax error file.json:1:1" "not well formatted file.json:1:1" and printing it gives an empty array. This files should be well written as I copyed them from online examples unless thwy all where missing some part of the file(something that's well possible as I don't know the structure of the file, while knowing the object properties". Also, saving the same files as a variable inside the sketch gives no problem, but in fact I need it as external file.

EDIT: I have the same problem with loadStrings, and I only tryed in locale with firefox developer ed


  • edited December 2015

    I've added these 2 lines below at the end of draw() from http://p5js.org/reference/#/p5/loadJSON:


    And I couldn't see anything wrong! (:|

    When asking folks to troubleshoot, it should be accompanied by running excerpts of the problem! :-w

  • edited December 2015

    Duh, while uploading it to my server to show it to you, it ended up working.......... #-o (#tattical facepalm).

  • edited December 2015

    yet when running in local, the program works,loads and reads properly the file, but there is a consistent "syntax error plugList.json:1:1" in the console, that isn't there in the version uploaded online;

    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},

    and data is retrieved as


    Works with no problem online but gives warnings on locale; Any reason?

    sketch ; source and the file is above

  • @ondsinet_: This is a FAQ. check for warnings about loading local files. Many browsers block this since it's a security risk...

  • edited January 2016 Answer ✓

    Made the shorter version below: $-)

    <script src=http://p5js.org/js/p5.min.js defer></script>
    var json;
    function preload() {
      json = loadJSON('plugList.json'); // XHR finished loading: GET "file:/// ...
    function setup() {
      console.log(json); // Array [ Object, Object, Object ]
      console.info(json[1]); // Object { firstName: "Anna", lastName: "Smith" }
      console.info(json[2].lastName[0]); // Object { a: "Jones", b: "Indi" }

    Executed the "index.html" file:/// above locally w/o any server artifice: :ar!

    1. Waterfox & Cyberfox: run w/o any problems.
    2. SlimJet w/ "--allow-file-access-from-files": no problems either.
    3. IE11: BLOCKED loading JSON even after clicking to allow it!
  • Answer ✓

    anyway anyhow it was partially the println command fault, as it showed an empty array, while console.log gives the expected result

  • In the beginning both print() & println() were alias for console.log().
    But some insanity idea happened there and they had decided to have their own poor implementation.
    Ah! It doesn't accept multiple args anymore! 3:-O

Sign In or Register to comment.