MagicMirror² v2.5.0 is available! For more information about this release, check out this topic.

Trim the fat in a JSON file?



  • So, I got my MM working. Set up a few modules and learning along the way.
    Than comes the JSON file that I get to display, but I only care about a part of it.
    Its a ComEd Hourly Pricing and the output it gives is

    0:[{"millisUTC":"1517732400000","price":"2.1"}]
    

    I just want the 2.1 output

    there is a “values” option that seems to be what I need to use,
    ie:

    Example: [“key1”, “key2”, “keyA.keyB.keyC”]

    but when I add the last line

    {
        module: 'MMM-json-feed',
        position: 'bottom_left',
        config: {
          url: 'https://hourlypricing.comed.com/api?type=currenthouraverage&format=json',
          title: "ComEd Hourly Pricing",
          values: ["key4"],
          
        }
      },
    

    it doesnt work.

    Am I doing something wrong?


  • Module Developer

    @DeathChicken said in Trim the fat in a JSON file?:

    I just want the 2.1 output

    So the value of the key “price”. I haven’t looked at the module’s code, but I think you have to say “price” instead of “key4”.

        values: ["price"]
    


  • Tried that. It doesn’t work.
    The field just doesn’t display any data at all
    alt text

    if i set

    values: [""]
    

    it shows alt text


  • Module Developer

    Ok, I checked the code of that module, values: ["price"] IS the correct setting for the config but getValue: function(data, value) {... has an error.

    getValue: function(data, value) {
        if (data && value) {
          var split = value.split(".");
          var current = data;
          while (split.length > 0) {
    //        current = current[split.shift()]; // WRONG!
              current = current[0][split.shift()];
          }
          return current;
        }
        return null;
    },
    

    I’d have written that totally different, but yeah, at the moment, the problem is, that the object it should be looking for is nested within a wrapping object. Add the [0]and you should be getting your results.
    Edit: Could also be a problem with the received json format.



  • Thank you so much.
    I wasn’t sure where to add the [0] exactly. But at least I had somethign to play with.
    After a couple tries values: [“0.price”] yields output of Price: “2.1” which is clear enough for my needs.



  • @doubleT If you think there is an improvement that can be done to that module, perhaps file an issue or PR at the module repo? I’ve been looking at that module and wondering if it’s worthwhile using it, or rather just use something more complete as a template. What do you think?