# Advice on JSON format

**URL:** <https://discourse.nodered.org/t/advice-on-json-format/74558>\
**Category:** General\
**Created:** [1 February 2023 13:25 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558 "2023-02-01T13:25:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2023 13:25 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/1 "2023-02-01T13:25:06Z")

</div>

Please can people tell me which of these JSON formats is the most appropriate?

1. Object.

```auto
{
  "services":
  {
    "nodered":{"State":"running","enabled":"enabled"},
    "mariadb":{"State":"running","enabled":"enabled"}
  }
}

```

1. Array.

```auto
{
  "services":
  [
    {"name":"nodered","State":"running","enabled":"enabled"},
    {"name":"mariadb","State":"running","enabled":"enabled"}
  ]
}

```

Using the first I can easily get msg.payload.services.nodered.State or msg.payload.services.mariadb.State

This is not so easy with the second, array format, but I think it will be easier to make a dashboard table to show the status.

My aim is a dashboard showing the status of multiple services as LEDs, grey for not installed, green for running, red for stopped.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 13:38 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/2 "2023-02-01T13:38:09Z")

</div>

Is there a right answer? 😅

My vote will be for an array - but that's just because I prefer to use arrays for table things  
(sorry I know that isn't helpful)

Will you need to specifically call `msg.payload.services.nodered.State` with in the dashboard?  
can you not do everything within a loop? `$(this).State`

Disclaimer; I don't use dashboards

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2023 13:44 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/3 "2023-02-01T13:44:56Z")

</div>

> [@marcus-j-davies](#):
>
> Is there a right answer?

I'm hoping so 😀

I am not sure that I'll _need_ `msg.payload.services.nodered.State`, it's just easy to feed into a debug node.

I hope I won't use `$(this).State` either, I'm allergic to any syntax including "this" or "scope"!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 February 2023 14:48 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/4 "2023-02-01T14:48:59Z")

</div>

My golden rule is to do whatever is simpler for me to implement, unless there is a powerful reason not to, which there usually is not.

Colin

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 February 2023 20:32 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/5 "2023-02-01T20:32:49Z")

</div>

> [@jbudd](#):
>
> ```auto
> {
> "services":
> {
> "nodered":{"State":"running","enabled":"enabled"},
> "mariadb":{"State":"running","enabled":"enabled"}
> }
> }
> 
> ```

Leaving aside the Dashboard question for a moment, I'd also suggest a tweak to this object version.

```auto
{
  "services":
  {
    "nodered":{"name":"nodered", "State":"running","enabled":"enabled"},
    "mariadb":{"name":"mariadb", "State":"running","enabled":"enabled"}
  }
}

```

The reason being that you can now easily process both as an object and an array depending on need.

Objects are best for reference by name. `msg.services.nodered` for example. But if you then needed to walk through all of the services, you might want to do something like:

```auto
Object.values(msg.services).forEach( service => {
    ...
})

```

Where having the name still inside the inner data - while slightly larger in size, still gives you access to the name without having to faff with looping through the keys and having to access the inner object by keyname.

I quite often use that format when I think I might want to process the data both as an object and an array.

If using the array format, if you want a single entry, you are forced to use a filter to process the array. the object format lets you access any entry directly. So I rarely use the array format unless I'm certain in advance that I will never need to reference a single entry.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2023 20:50 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/6 "2023-02-01T20:50:33Z")

</div>

That's nicely thought out and explained. A useful technique, thanks 😀

My data passed from Pi to Node-red is more than just installed services. It's structure is not yet set , currently it looks like this.

```auto
{
  "services":
  [
    {"Id":"mariadb.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"}, 
     {"Id":"nodered.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"}, 
     {"Id":"mosquitto.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"},
     {"Id":"influxdb.service","ActiveState":"inactive","SubState":"dead","UnitFileState":""},
     {"Id":"log2ram.service","ActiveState":"inactive","SubState":"dead","UnitFileState":""}
  ],
  "memory":{"units":"MB","total":3793,"used":226,"free":3124,"available":3492},
  "disk":
  [
    {"filesystem":"/dev/root","mount":"/","size":112390,"used":3594,"available":104196,"percentused":4,"units":"MB"},
    {"filesystem":"/dev/sda1","mount":"/boot","size":255,"used":31,"available":225,"percentused":12,"units":"MB"}
  ],
  "ip":"192.168.1.12",
  "pihole":"enabled",
  "overlayFS":"Not installed"
}

```

The whole thing is so ugly that my inner protest "but name mariadb inside a mariadb object is redundant" is very easy to ignore!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 February 2023 21:09 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/7 "2023-02-01T21:09:31Z")

</div>

You have awoken the tidy freak In me now!

You could partition it further?

Might not be for some, but dealing with .NET namespaces in my professional setting, I couldn't miss the opportunity 😅

```auto
{
  "services": [
     {"Id":"mariadb.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"}, 
     {"Id":"nodered.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"}, 
     {"Id":"mosquitto.service","ActiveState":"active","SubState":"running","UnitFileState":"enabled"},
     {"Id":"influxdb.service","ActiveState":"inactive","SubState":"dead","UnitFileState":""},
     {"Id":"log2ram.service","ActiveState":"inactive","SubState":"dead","UnitFileState":""}
  ],
  "utilisation":{
     "memory":{"units":"MB","total":3793,"used":226,"free":3124,"available":3492},
     "storage":{
        "overlayFS": "not enabled",
        "volumes": [
          {"filesystem":"/dev/root","mount":"/","size":112390,"used":3594,"available":104196,"percentused":4,"units":"MB"},
          {"filesystem":"/dev/sda1","mount":"/boot","size":255,"used":31,"available":225,"percentused":12,"units":"MB"}
        ]
    }
   },
   "network":{
      "ip":"192.168.1.12",
      "pihole":"enabled"
    }
}

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2023 21:50 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/8 "2023-02-01T21:50:32Z")

</div>

Thing is, the different sections are ripped from the output of different commands - services from  
systemctl show nodered --property=Id,ActiveState,SubState,UnitFileState,  
memory from free -m etc.

At the moment thay are just concatenated.

One day they'll all get displayed in an tidy and elegant dashboard.

If only I could discipline myself not to juggle SD cards and Pies I could just remember which one has what.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 April 2023 21:50 UTC](https://discourse.nodered.org/t/advice-on-json-format/74558/9 "2023-04-02T21:50:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
