Navigation

    MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord
    MagicMirror² v2.15.0 is available! For more information about this release, check out this topic.

    Trim the fat in a JSON file?

    Troubleshooting
    3
    6
    1896
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      DeathChicken last edited by DeathChicken

      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?

      1 Reply Last reply Reply Quote 0
      • D
        doubleT Module Developer last edited by

        @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"]
        
        1 Reply Last reply Reply Quote 1
        • D
          DeathChicken last edited by DeathChicken

          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

          1 Reply Last reply Reply Quote 0
          • D
            doubleT Module Developer last edited by doubleT

            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.

            E 1 Reply Last reply Reply Quote 2
            • D
              DeathChicken last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • E
                E3V3A @doubleT last edited by

                @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?

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Enjoying MagicMirror? Please consider a donation!
                MagicMirror created by Michael Teeuw.
                Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                This forum is using NodeBB as its core | Contributors
                Contact | Privacy Policy