timdows / MMM-JsonTable

A module for the MagicMirror project which creates a table filled with a list gathered from a json request.
MIT License
25 stars 31 forks source link

fetching data from array withing an array #79

Closed late4marshmellow closed 1 year ago

late4marshmellow commented 1 year ago

Hey @timdows thank you for this great module!

Maybe i am doing something wrong? i am trying to get the data from "mres" and "Forbruk" to be displayed on two different modules. The config i've been trying to use is under arrayName:

tried same for "Forbruk"

am i missing something?

{"status":"success","url":"/soppel","method":"GET","data":[{"Forbrok":[{"deviceName":"Power consumption","currentValue":"Now: 3009 watts","currentTarget":" ","lastUpdate":"Limit: 5000 watt"},{"deviceName":"Varmepumpe","currentValue":"Now:25.8°C,","currentTarget":"Target:21°C,","lastUpdate":"Home override: true"},{"deviceName":"Hotwater","currentValue":"Now:44.38°C,","currentTarget":"Last heat complete","lastUpdate":"66.9 : 05:46 @ 19-12-2022 "},{"deviceName":"Last update","currentValue":" ","currentTarget":" ","lastUpdate":"23:27 19-12-2022"}]},{"mren":[{"avfallstype":"Restavfall","dato":"30-12-2022"},{"avfallstype":"Matavfall","dato":"30-12-2022"},{"avfallstype":"Papiravfall","dato":"30-12-2022"},{"avfallstype":"Plastemballasje","dato":"30-12-2022"},{"avfallstype":"Glass- og metallemballasje","dato":"30-12-2022"}]}]}

late4marshmellow commented 1 year ago

solved this by adding this lines to config.js

    arrayName: "data", // Optional
    arrayName2: "Forbruk", // Optional

and this to MMM-JsonTable.js

  // Default module config.
  defaults: {
    url: "",
    arrayName: null,
    arrayName2: null,

and this on line 62

let items = [];
if (this.config.arrayName) {
  if (this.config.arrayName2) {
    items = this.jsonData[this.config.arrayName][this.config.arrayName2];
  } else {
    items = this.jsonData[this.config.arrayName];
  }
} else {
  items = this.jsonData;
}