# Extract data from object based on its place in the object

**URL:** <https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040>\
**Category:** General\
**Created:** [22 January 2023 08:19 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040 "2023-01-22T08:19:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimC1986](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/timc1986/32/63085_2.png) [@TimC1986](https://discourse.nodered.org/u/TimC1986)\
**Post date:** [22 January 2023 08:19 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/1 "2023-01-22T08:19:44Z")

</div>

Hello,

How can I use a value from a object, based on its place in the object? I use a API that returns a result:  
 ![payload](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a437d45e0c59436bbf9f20297d2ef05013dca70.png)

the problem is that the date is part of the object and will change every day.

I'm only interested in thevalue 2nd date "2023-01-22" in this flow

```auto
[
    {
        "id": "67ae90406b6c9f81",
        "type": "inject",
        "z": "b29659afe04a7d40",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"result\":{\"2023-01-21\":18734,\"2023-01-22\":15786},\"message\":{\"code\":0,\"type\":\"success\",\"text\":\"\",\"info\":{\"latitude\":50.8154,\"longitude\":3.3773,\"distance\":0,\"place\":\"62, Otegemse steenweg, Deerlijk, Kortrijk, West-Vlaanderen, Vlaanderen, 8540, België / Belgique / Belgien\",\"timezone\":\"Europe/Brussels\",\"time\":\"2023-01-21T15:51:13+01:00\",\"time_utc\":\"2023-01-21T14:51:13+00:00\"},\"ratelimit\":{\"period\":3600,\"limit\":12,\"remaining\":11}}}",
        "payloadType": "jsonata",
        "x": 350,
        "y": 200,
        "wires": [
            [
                "4c35102c3b553ede"
            ]
        ]
    }
]

```

Is ther a way to isolate thet value of the 2nd date in the object?  
I checked already a lot of research but with no succes

Kind regards Tim

---

<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:** [22 January 2023 08:47 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/2 "2023-01-22T08:47:43Z")

</div>

No doubt it's possible with fewer nodes, JSONata, but this should work.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8de66b10f5ebeac885eaf79f7a05ef261791979.jpeg)

```auto
[{"id":"a5b6d2f4c0c645fe","type":"function","z":"7726835ff669c023","name":"Sample data","func":"msg.payload = { result: \n {\"2023-01-21\": 18734,\n \"2023-01-22\": 15786}\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":100,"wires":[["2c0d74997b2c801c"]]},{"id":"4ee96b3158372633","type":"inject","z":"7726835ff669c023","name":"go","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":100,"wires":[["a5b6d2f4c0c645fe"]]},{"id":"a54f2807009aca9b","type":"split","z":"7726835ff669c023","name":"split on ,","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":620,"y":100,"wires":[["5ef2772f4b4b557c"]]},{"id":"2c0d74997b2c801c","type":"change","z":"7726835ff669c023","name":"Just .result","rules":[{"t":"move","p":"payload.result","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":100,"wires":[["a54f2807009aca9b"]]},{"id":"5ef2772f4b4b557c","type":"switch","z":"7726835ff669c023","name":"index 1","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":100,"wires":[["0fba146143bb4af8","7bf23c501aed9df2"]]},{"id":"0fba146143bb4af8","type":"debug","z":"7726835ff669c023","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":100,"wires":[]},{"id":"7bf23c501aed9df2","type":"debug","z":"7726835ff669c023","name":"parts.key","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":140,"wires":[]}]

```

---

<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:** [22 January 2023 09:52 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/3 "2023-01-22T09:52:30Z")

</div>

The JSONata expression would be  
`$keys($$.payload.result)[1]`  
example

```auto
[{"id":"67ae90406b6c9f81","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"result\":{\"2023-01-21\":18734,\"2023-01-22\":15786},\"message\":{\"code\":0,\"type\":\"success\",\"text\":\"\",\"info\":{\"latitude\":50.8154,\"longitude\":3.3773,\"distance\":0,\"place\":\"62, Otegemse steenweg, Deerlijk, Kortrijk, West-Vlaanderen, Vlaanderen, 8540, België / Belgique / Belgien\",\"timezone\":\"Europe/Brussels\",\"time\":\"2023-01-21T15:51:13+01:00\",\"time_utc\":\"2023-01-21T14:51:13+00:00\"},\"ratelimit\":{\"period\":3600,\"limit\":12,\"remaining\":11}}}","payloadType":"json","x":230,"y":4200,"wires":[["f7c765e3a489d940"]]},{"id":"f7c765e3a489d940","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$keys($$.payload.result)[1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":4200,"wires":[["0fba146143bb4af8"]]},{"id":"0fba146143bb4af8","type":"debug","z":"da8a6ef0b3c9a5c8","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":4200,"wires":[]}]

```

Or Javascript in a function node

```auto
msg.payload = Object.keys(msg.payload.result)[1]

```

---

<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:** [22 January 2023 10:00 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/4 "2023-01-22T10:00:56Z")

</div>

@E1cid@jbudd Are you sure you can rely on the order of the keys? Doesn't it depend on how the object was constructed? I think it may be necessary to sort the keys first. A simple alpha type search will be ok.

@TimC1986 do you have any control over the data? Where is it coming from? It would be better if the data were in a sorted array, something like

```auto
[
  {date: "2023-01-21", value: 18734},
  {date: "2023-01-22", value: 15786}
]

```

Then you simply use `msg.payload[1].date`.

---

<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:** [22 January 2023 10:22 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/5 "2023-01-22T10:22:24Z")

</div>

Colin you know very well that you can not be certain dictionaries(objects) order, and you know both @jbudd and myself know it to.

If the order of the return object can not be trusted, i.e the device sending alters it, which i doubt happens in this case, you can use

```auto
(
   $keys($$.payload.result).{
       "date": $,
       "value": $lookup($$.payload.result, $)
   }
)^(date)[1].date

```

---

<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:** [22 January 2023 10:43 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/6 "2023-01-22T10:43:42Z")

</div>

Yes, probably wise to sort on msg.parts.key. One more node 🥱

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/752054083d4b7ada1aea0938a23b963f35e8e8ad.jpeg)

I wonder which of the 3 methods would be fastest if the payload object was of considerable size.  
The javascript function wins on screen real estate and readability.

---

<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:** [22 January 2023 13:17 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/7 "2023-01-22T13:17:39Z")

</div>

> [@E1cid](#):
>
> you know very well that you can not be certain dictionaries(objects) order,

Well actually I wasn't certain what the current position is. I remember seeing recently that there was a change to the language that did enforce an order in some way, but I wasn't sure what it was.

---

<div class="post-metadata">

**Author:** ![TimC1986](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/timc1986/32/63085_2.png) [@TimC1986](https://discourse.nodered.org/u/TimC1986)\
**Post date:** [23 January 2023 05:36 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/8 "2023-01-23T05:36:45Z")

</div>

Hi thanks for the helping hand, works perfect!

---

<div class="post-metadata">

**Author:** ![TimC1986](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/timc1986/32/63085_2.png) [@TimC1986](https://discourse.nodered.org/u/TimC1986)\
**Post date:** [23 January 2023 05:39 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/9 "2023-01-23T05:39:22Z")

</div>

Hi Colin, no I don't have control over the data, it comes from a API from a solar forecast service.

Thanks for the help I will check this out

---

<div class="post-metadata">

**Author:** ![TimC1986](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/timc1986/32/63085_2.png) [@TimC1986](https://discourse.nodered.org/u/TimC1986)\
**Post date:** [23 January 2023 05:43 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/10 "2023-01-23T05:43:42Z")

</div>

Thanks E1cid, I tried the flow but works not directly, but I will look on it in detail and can definitly learn something with it. Thanks

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [23 January 2023 20:56 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/11 "2023-01-23T20:56:16Z")

</div>

> [@E1cid](#):
>
> `)^(date)[1].date`

I think you wanted the value not the date so replace `.date` with `.value`

---

<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:** [23 January 2023 21:14 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/12 "2023-01-23T21:14:33Z")

</div>

> [@Buckskin](#):
>
> I think you wanted the value not the date so replace `.date` with `.value`

No I meant to use `.date`, as that what I believe the OP specified. two other contributors to this thread seem to think the same, we may have misunderstood the OP's post though.

> [@TimC1986](#):
>
> I'm only interested in thevalue 2nd date "2023-01-22" in this flow

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [23 January 2023 21:30 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/13 "2023-01-23T21:30:02Z")

</div>

Yes, but that was the only reason that I could see why he said that your ingenious code wouldn't work. It took me ages pouring through the JSONata how-to to work out what you had done. 😀

---

<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:** [23 January 2023 22:35 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/14 "2023-01-23T22:35:51Z")

</div>

Your comment makes more sense to me know. Cheers

---

<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:** [6 February 2023 22:36 UTC](https://discourse.nodered.org/t/extract-data-from-object-based-on-its-place-in-the-object/74040/15 "2023-02-06T22:36:37Z")

</div>

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