# JSONata value - keyname

**URL:** https://discourse.nodered.org/t/jsonata-value-keyname/76175
**Category:** General
**Tags:** jsonata
**Created:** [6 March 2023 12:05 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175 "2023-03-06T12:05:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 March 2023 12:05 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/1 "2023-03-06T12:05:17Z")

</div>

I have trouble with jsonata.

Given this array:

```auto
[
  {
    "_ansible_item_label": "hostname.stdout",
    "_ansible_no_log": null,
    "_ansible_verbose_always": true,
    "ansible_loop_var": "item",
    "changed": false,
    "failed": false,
    "hostname.stdout": "cinema",
    "item": "hostname.stdout"
  },
  {
    "_ansible_item_label": "uptime.stdout",
    "_ansible_no_log": null,
    "_ansible_verbose_always": true,
    "ansible_loop_var": "item",
    "changed": false,
    "failed": false,
    "item": "uptime.stdout",
    "uptime.stdout": "22 days"
  }
]

```

I want to get the values from the properties that are given in `item`.

Hardcoded I would use `hostname.stdout` - but this property name is not known up front, so it is very useful that it is available in the `item` property, but how can i dynamically get the `item` value and use that to read the property ? trying to use the backticks with item but that selects all 'items' as an array.

End result should become something like:

```auto
{
  "hostname": "cinema",
  "uptime": "22 days"
}

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 March 2023 12:19 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/2 "2023-03-06T12:19:51Z")

</div>

Not entirely sure if I understand your question but assuming you can use a function instead of JSONata, does this work for you?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/197aece0853d71222ee6ada5084cc40d9d9a1495.png)

```auto
const parentItem = data.find(e => e.item === "hostname.stdout")
const childItem = data.find(e => e.item === "uptime.stdout")
const hostname = parentItem["hostname.stdout"]
const uptime = childItem["uptime.stdout"]
msg.payload = { hostname, uptime }
return msg

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 March 2023 12:43 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/3 "2023-03-06T12:43:22Z")

</div>

Thanks, I was not sure how to formulate the question.

To rephrase it: I do not know the property name `hostname.stdout` upfront (it could be anything like `example.stdout`), but `item` contains the property name - the value of `item` _describes_ the property name I want to read.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 March 2023 12:52 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/4 "2023-03-06T12:52:29Z")

</div>

Ah ok, now I understand.

```auto
const data = msg.payload
const payload = {}
data.forEach(e => {
  const niceName = e.item.split('.')[0]
  payload[niceName] = e[e.item]
})
msg.payload = payload
return msg

```

(Untested)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [6 March 2023 13:46 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/5 "2023-03-06T13:46:07Z")

</div>

The JSONata would be

```auto
$$.payload.${$split($.item, ".")[0]: $lookup($,$.item)}

```

output

```auto
{
    "hostname": "cinema",
    "uptime": "22 days"
}

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 March 2023 14:34 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/6 "2023-03-06T14:34:37Z")

</div>

> [@E1cid](#):
>
> `${$split($.item, ".")[0]: $lookup($,$.item)}`

Thanks alot indeed this is the right direction, but because I did not provide my full output thinking I could manage it, but I cannot make it work "perfectly".

The json object is much larger and the array is nested. [See the output on try.jsonata](https://try.jsonata.org/bg88kn7rw)

Now i have the following:

```auto
**[msg='All items completed'].[
    $.results.${$split($.item, ".")[0]: $lookup($,$.item)}
]

```

which produces:

```auto
[
  [
    {
      "hostname": "cinema",
      "uptime": "22 days"
    }
  ],
  [
    {
      "hostname": "homebridge",
      "uptime": "22 days"
    }
  ]
]

```

I would like to get rid of the surrounding arrays if possible. The "all items completed" may also not be the best thing to filter on, as I don't know what it produces when it fails at a step (yet to test). Perhaps the last element in the task array, as it will always be the last element that contains the end results of the tasks it executed (this comes from an ansible playbook btw).

@Steve-Mcl Thanks, although untested, by the looks of it this might work too, I might resort to a function node if this doesnt work out.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [6 March 2023 14:50 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/7 "2023-03-06T14:50:43Z")

</div>

does this work

```auto
**[msg='All items completed'].[
    $.results.${$split($.item, ".")[0]: $lookup($,$.item)}
][[0]]

```

or

```auto
**[msg='All items completed'].(
    $.results.${$split($.item, ".")[0]: $lookup($,$.item)}
)

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 March 2023 15:41 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/8 "2023-03-06T15:41:03Z")

</div>

Nice the second one works perfectly, thanks!

---

<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: [20 March 2023 15:41 UTC](https://discourse.nodered.org/t/jsonata-value-keyname/76175/9 "2023-03-20T15:41:08Z")

</div>

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