[ANSWERED] How do I Read and Decode a JSON Text File?

  • Beta Platinum

    [EDIT: edited the name of this thread to better indicate what the actual question is. -- Mark]

    Hi @DusX 

    I am having issues with the javascript actor on MBP 16" i9, Mac OSX 10.15.7 Catalina, Isadora 3.0.7 - 

    I am attempting to use the include txt file with javascript actor and am getting nowhere with parsing the simple code indicated in the tutorial "How to load external data with javascript".

    I am seeking to read .json files generated by Meshlab (example included below). As an extension .json is not included in the tutorial, I have attempted to change the extension to .txt - however, I am no closer to getting preformatted json files into Isadora. Any advice would be appreciated.

    fib.json.zip

    On top of that, I am experiencing some unstable behaviour with the javascript actor within the scene editor. 

    Best Wishes

    Russell

  • Tech Staff

    @bonemap

    This file uses read() to place the text contents of the json file into a variable. (include() runs the file contents as Javascript)
    Needs to be in the same folder as your JSON file.

    FIB-v1.izz

  • Beta Platinum

    @dusx said:

    uses read() to place the text contents of the json file into a variable

     This is great. Is there a simple fix so that it reads the negative floats as well as the positive?

    Best Wishes

    Russell

  • Tech Staff

    @bonemap said:

    fix so that it reads the negative floats

     OK, I missed that. I'm not sure. I just added a couple Print statements to make sure the data is read correctly, and that seems fine.
    So I have gone about breaking down the data another way (in steps) and I can see the last parsing step, where the ARRAY is parsed is where the negative signs are lost.
    I found this not so helpful article: javascript - JSON.parse fails for negative floating point numbers - Stack Overflow which suggest perhaps the '-' signs are of the wrong type.
    Since the Array is correct until the last step in this new file, perhaps some string parsing could be done to replace the '-' with a good one (if that's the case). Weirdly JSON validators are saying the JSON is good.
    FIB-v1.izz (same name).. just a quick update

  • Tech Staff

    Example Reading a JSON file with JavaScript / Parsing Object

    So in the following example, we are able to actually load in the JSON file and parse the object it gives with the 'vertices' field. (Why can't I select this with the JSON parser, feature request @DusX ?) Then we are able to actually take it further apart with the default JSON actors of Isadora.

  • Tech Staff

    @juriaan

    And of course, if you wish to parse the text to a valid number we just have to add this to a Text parser.

    value:float=.#

  • Beta Platinum

    @juriaan said:

    select this with the JSON parser, feature request

     Yes, I thought this too. A feature that allowed loading preformatted json files into the json plugin makes a lot of sense.

    Thank you for the contribution.

    Best wishes

    Russell

  • Tech Staff

    @bonemap

    This new file, mixes some of Juriaans', in a new approach that gets you your data. (2 methods shown, but the JS version is better in that I am returning the Array Length as well).

    I think you should be able to work with this. FIB-v2.izz

  • Beta Platinum

    @dusx said:

    Array Length

     Yes, the array length is a useful / necessary parameter. Good to have!

    Best wishes

    Russell 


  • @dusx @bonemap @juriaan

    @juriaan asked:

    Why can't I select this with the JSON parser, feature request @DusX?

    Answer: you can.

    I am sorry I'm joining this discussion so late, as it seems you all have gone through some gyrations that were really not necessary.

    First, I thought the File Reader plugin was on the Add Ons (formally known as Plugins) page... but it's not. It is attached below. (I'll post it to the Add Ons page once you all give me some feedback that it's working OK.) This actor simply reads a file and outputs the text, which addresses the issue of getting the file data into the JSON Parser. (We can discuss whether or not file reading should be a built in thing on the JSON parser.. but for now, we'll let the File Reader plugin do it, OK?)

    With the file reading addressed, I want to remind everyone that you can access any element in the JSON structure by using colon separated element names. Look at the help text for the 'select 1' input:

    A string in the format xxx:yyy:zzz that selects a data member for the corresponding output, where xxx, yyy and zzz, can either be a key value or a numeric index. Items with only numeric digits are interpreted as an index. Items with non-numeric characters are interpreted as a key. Keys that consist only of numeric characters or that contain a colon must be enclosed in double quotes.
    For example, the selector mark:3:leg 1 would attempt to get the top level element named 'mark', the third item inside of 'mark', and then a keyed value called 'leg 1' within that element.

    Here's the text of Bonemap's original JSON file.

    (FYI: the minus signs are totally OK and did not need to be converted, as Bonemap guessed above.)

    {
      "version" : "0.1.0",
      "comment" : "Generated by MeshLab JSON Exporter",
      "id"      : 1,
      "name"    : "mesh",
      "vertices" :                             **** VERTICES ELEMENT *****
      [                                        **** ARRAY ELEMENT 0 *****
        {
          "name"       : "position_buffer",
          "size"       : 3,
          "type"       : "float32",
          "normalized" : false,
          "values"     :                       ***** VALUES ELEMENT ****
          [
            0.141067, 0, 0.99, -0.179258, -0.164215, 0.97, 0.0272986, 0.311054, 0.95, 0.223637,
            -0.419697, -0.409579, -0.81, 0.0311997, 0.55689, -0.83, 0.333553, -0.407729, -0.856537
          ]
        }
      ],
      "connectivity" :
      [
      ],
      "mapping" :
      [
      ],
      "custom" : null
    }
    

    I've marked the elements with asterisk enclosed labels above (these "comments" are not valid JSON!!!)

    Grabbing the elements with names should be clear. But note that the inside of the "values" element is an array (marked by the square brackets) with one element. Array indexes are always zero based in the JSON Parser, so we use index 0 to get the array element, since it has no label.

    With that in mind, to get that the list of values, you simply need to enter this in the 'select 1' input of the JSON Parser actor.:

    vertices:0:values

    and voilá, out come the 300 values stored in the fib.json file Bonemap originally posted.

    I use a second JSON Parser actor to actually pull out the individual values. (This would be more efficient if the original JSON was really huge.)

    So please try installing the File Reader plugin and then take a look at my sample patch below.

    Best Wishes,
    Mark

    file-reader-v0.9.2.zip

    read-json-with-file-reader-actor.zip

  • Beta Platinum

    @mark said:

    feedback that it's working

     Hi,

    Brilliant! 

    I will do some testing exchanging json files on the fly and see how that then propagates throughout a patch.

    Best Wishes

    Russell