# JSONata - am I missing something?

**URL:** <https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464>\
**Category:** General\
**Created:** [18 October 2020 16:42 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464 "2020-10-18T16:42:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [18 October 2020 16:42 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/1 "2020-10-18T16:42:23Z")

</div>

I have a simple JSON

```auto
{
  "customers": [
    {
      "id": "XYZ-ABC-DEF",
      "data": {
        "locations": [
          {
            "location_id": 1,
            "location_name": "Main",
            "location_attr": "blue"
          },
          {
            "location_id": 2,
            "location_name": "Secondary"
          }
        ]
      }
    }
  ]
}

```

I do a simple op:

```auto
customers[][data.locations.location_attr = "blue"]

```

And it works, returns the entire 'customer' object. But only as long as only one record has the "location\_attr" field. As soon as I add "location\_attr" to a second record - it stops working.

But a different op is still possible:

```auto
customers.data.locations[location_attr = "blue"]

```

However, this defeats the purpose - I need the customer id.

It's driving me nuts 🙂 Please point me to the explanation/solution!

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [18 October 2020 17:13 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/2 "2020-10-18T17:13:29Z")

</div>

> [@bogorad](#):
>
> ```auto
> customers.data.locations[location_attr = "blue"]
> 
> ```

What about this:

```auto
customers.data.locations[location_attr = "blue"].$$

```

And possibly:

```auto
customers.data.locations[location_attr = "blue"].$$.customers.id

```

To only get the id.

Please, don't ask for an explanation... but $$ and $ are "magical" (at least for me) in Jsonata.

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [18 October 2020 18:25 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/3 "2020-10-18T18:25:47Z")

</div>

Thanks! Your suggestion looked promising, but matches the whole structure ☹ I need only the customer with the attribute.

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [18 October 2020 21:27 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/4 "2020-10-18T21:27:54Z")

</div>

So it looks like a limitation ☹

I had to get rid of embedded structures and add redundancy to the data.

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [29 October 2020 20:15 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/5 "2020-10-29T20:15:54Z")

</div>

Using your json, what exact output do you need?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [29 October 2020 21:12 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/6 "2020-10-29T21:12:07Z")

</div>

> [@bogorad](#):
>
> So it looks like a limitation ☹

Just stumbled upon your jsonata riddle.

Here is the solution I think:

```auto
customers[][data.locations.location_attr[$="blue"]!=[]]

```

you can check it with following url: [https://try.jsonata.org/LimnK1l7D](https://try.jsonata.org/LimnK1l7D)

**Explanation:**  
It returns all `customers` matching function :

```auto
data.locations.location_attr[$="blue"] != []

```

- The left part (`data.locations.location_attr[$="blue"]`) of the function creates an array of all `location_attr` values belonging to that customer that have value `"blue"`.
- The right part (`[]`) is an empty array.

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [31 October 2020 10:02 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/7 "2020-10-31T10:02:50Z")

</div>

Thank you so much! So it looks like this '$' is indeed the magic bullet.

---

<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:** [14 November 2020 10:02 UTC](https://discourse.nodered.org/t/jsonata-am-i-missing-something/34464/8 "2020-11-14T10:02:50Z")

</div>

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