• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Trim the fat in a JSON file?

Scheduled Pinned Locked Moved Troubleshooting
6 Posts 3 Posters 2.5k Views 4 Watching
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 Offline
    DeathChicken
    last edited by DeathChicken Feb 4, 2018, 9:05 AM Feb 4, 2018, 8:49 AM

    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 Offline
      doubleT Module Developer
      last edited by Feb 4, 2018, 2:38 PM

      @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 Offline
        DeathChicken
        last edited by DeathChicken Feb 4, 2018, 6:10 PM Feb 4, 2018, 5:10 PM

        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 Offline
          doubleT Module Developer
          last edited by doubleT Feb 5, 2018, 5:30 PM Feb 5, 2018, 5:27 PM

          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 Feb 9, 2018, 11:10 AM Reply Quote 2
          • D Offline
            DeathChicken
            last edited by Feb 7, 2018, 1:14 AM

            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 Offline
              E3V3A @doubleT
              last edited by Feb 9, 2018, 11:10 AM

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

              "Everything I do (here) is for free – altruism is the way!"
              MMM-FlightsAbove, MMM-Tabulator, MMM-Assistant (co-maintainer)

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              1 / 1
              • First post
                4/6
                Last post
              Enjoying MagicMirror? Please consider a donation!
              MagicMirror created by Michael Teeuw.
              Forum managed by Sam, technical setup by Karsten.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy