How to combine two objects data with one same variable value

Hi, I'm new to both nodered and javascript.

I have two data objects retrieved from two sources. For example, msg.payload1 describe the location of the place and msg.payload2 describes the temperature around the place.

I want to combine the objects with the same place name so that the new data object consists of both the location and temperature.

for example,

I used the join node, the flow is:

But I can't merge the object with the same "place name".

How can I do that?

Thank you so much!

What do you want the result to look like?

This article in the cookbook is an example of how to join messages into one object using key/value mode, which I think will do what you want.

Is there any reason not to do these things in series, and just extend the msg object at each step ?

because the two data objects are got from two different APIs by HTTP request node, I don't know how to extend them. Is there any method?

the result is just like the image named"after combine". I want the object with same "place" to merge tgt, so the attributes can extend

thank you, but this example only handles the inject node. In my problem, I first get 2 objects from 2 APIs by using the HTTP request node, then I put the obtained data object in a function node to retrieve the attributes I needed. Then, I want to combine 2 objects from 2 payloads.

Could you please give me some examples on how to do that?

Thank you so much

What does the output of your join node look like?
Copy and paste the data rather than a picture please!

Here is the flow.

I have two data objects get from two APIs. Object 1 returns the payload1, and has 5 attributes. Object 2 returns the payload2 and has 2 attributes. Both objects 1 and 2 have the same attribute "place". I want to merge the object when they have the same value of "place". So the final data object will have 6 attributes (because the "place" is same).

How can I do it? Thank you so much!

[
    {
        "id": "3c5de79e.539108",
        "type": "tab",
        "label": "flow 1",
        "disabled": true,
        "info": ""
    },
    {
        "id": "eb9b21ca8f6c20f9",
        "type": "http request",
        "z": "3c5de79e.539108",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://www.geodetic.gov.hk/lpapi/?db=all",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 270,
        "y": 180,
        "wires": [
            [
                "4ee93f399d7d6d1f",
                "a1f436e1f8831be3"
            ]
        ]
    },
    {
        "id": "679763c87210febb",
        "type": "inject",
        "z": "3c5de79e.539108",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "600",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 180,
        "wires": [
            [
                "eb9b21ca8f6c20f9",
                "fc28f3aa6bf7f897"
            ]
        ]
    },
    {
        "id": "4ee93f399d7d6d1f",
        "type": "split",
        "z": "3c5de79e.539108",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 450,
        "y": 180,
        "wires": [
            [
                "85fc228bc02bcce5"
            ]
        ]
    },
    {
        "id": "a1f436e1f8831be3",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check object",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 290,
        "y": 80,
        "wires": []
    },
    {
        "id": "85fc228bc02bcce5",
        "type": "loop",
        "z": "3c5de79e.539108",
        "name": "",
        "kind": "fcnt",
        "count": "",
        "initial": "",
        "step": "1",
        "condition": "",
        "conditionType": "js",
        "when": "before",
        "enumeration": "enum",
        "enumerationType": "msg",
        "limit": "",
        "loopPayload": "loop-index",
        "finalPayload": "final-last",
        "x": 630,
        "y": 180,
        "wires": [
            [
                "0a7d2f8b690e2662"
            ],
            [
                "0a7d2f8b690e2662"
            ]
        ]
    },
    {
        "id": "0a7d2f8b690e2662",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "getname",
        "func": "var lampLocation = {\n    payload:{\n        name: msg.payload.LP_NUM,\n        north: msg.payload.LP_NORTH,\n        east: msg.payload.LP_EAST,\n        gl: msg.payload.LP_GL,\n        lat: msg.payload.LP_LAT,\n        lon: msg.payload.LP_LONG,\n    }\n};\n\nflow.set('lampLocation', lampLocation.payload);\n\nreturn lampLocation;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 180,
        "wires": [
            [
                "f6e5883a426a47ee"
            ]
        ]
    },
    {
        "id": "f6e5883a426a47ee",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check getLocation",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 910,
        "y": 280,
        "wires": []
    },
    {
        "id": "fc28f3aa6bf7f897",
        "type": "http request",
        "z": "3c5de79e.539108",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://www.hko.gov.hk/common/hko_data/R3/files/smart_lamppost_met_device_location.json",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 330,
        "y": 580,
        "wires": [
            [
                "66a9628cfafd3360"
            ]
        ]
    },
    {
        "id": "66a9628cfafd3360",
        "type": "split",
        "z": "3c5de79e.539108",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 510,
        "y": 580,
        "wires": [
            [
                "bceabcb6ef9c199e",
                "b9fa71faa1beab3c"
            ]
        ]
    },
    {
        "id": "bceabcb6ef9c199e",
        "type": "loop",
        "z": "3c5de79e.539108",
        "name": "",
        "kind": "fcnt",
        "count": "",
        "initial": "",
        "step": "1",
        "condition": "",
        "conditionType": "js",
        "when": "before",
        "enumeration": "enum",
        "enumerationType": "msg",
        "limit": "",
        "loopPayload": "loop-index",
        "finalPayload": "final-last",
        "x": 730,
        "y": 580,
        "wires": [
            [
                "9bbe6f75b3d0e07c"
            ],
            [
                "9bbe6f75b3d0e07c"
            ]
        ]
    },
    {
        "id": "b9fa71faa1beab3c",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check lamp type object",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 510,
        "y": 500,
        "wires": []
    },
    {
        "id": "9bbe6f75b3d0e07c",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "getIDType",
        "func": "return {\n    payload: {\n        lamppost_id: msg.payload.LP_NUMBER,\n        lamppost_type: msg.payload.LP_TYPE,\n        lat: msg.payload.LP_LATITUDE,\n        lon: msg.payload.LP_LONGITUDE\n    }\n};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 920,
        "y": 580,
        "wires": [
            [
                "44d012a392194a77",
                "3390fbfd129e6e6c"
            ]
        ]
    },
    {
        "id": "44d012a392194a77",
        "type": "switch",
        "z": "3c5de79e.539108",
        "name": "getDeviceID",
        "property": "payload.lamppost_type",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "01",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "02",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1130,
        "y": 580,
        "wires": [
            [
                "47645c984b40cd47"
            ],
            [
                "9a7105c941f5a1d0"
            ]
        ]
    },
    {
        "id": "3390fbfd129e6e6c",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check idtype",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 930,
        "y": 500,
        "wires": []
    },
    {
        "id": "47645c984b40cd47",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "getIDType",
        "func": "return {\n    payload: {\n        lamppost_id: msg.payload.lamppost_id,\n        lamppost_type: msg.payload.lamppost_type,\n        lat: msg.payload.lat,\n        lon: msg.payload.lon,\n        device_id: \"01\"\n    }\n};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1320,
        "y": 540,
        "wires": [
            [
                "6d629b022f73577e",
                "71402cdd2b25df5f"
            ]
        ]
    },
    {
        "id": "9a7105c941f5a1d0",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "getIDType",
        "func": "return {\n    payload: {\n        lamppost_id: msg.payload.lamppost_id,\n        lamppost_type: msg.payload.lamppost_type,\n        lat: msg.payload.lat,\n        lon: msg.payload.lon,\n        device_id: \"04\"\n    }\n};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1320,
        "y": 600,
        "wires": [
            [
                "bf617086f0d43abc",
                "71402cdd2b25df5f"
            ]
        ]
    },
    {
        "id": "6d629b022f73577e",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check deviceID01",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1250,
        "y": 440,
        "wires": []
    },
    {
        "id": "71402cdd2b25df5f",
        "type": "loop",
        "z": "3c5de79e.539108",
        "name": "",
        "kind": "fcnt",
        "count": "",
        "initial": "",
        "step": "1",
        "condition": "",
        "conditionType": "js",
        "when": "before",
        "enumeration": "enum",
        "enumerationType": "msg",
        "limit": "",
        "loopPayload": "loop-index",
        "finalPayload": "final-last",
        "x": 1530,
        "y": 560,
        "wires": [
            [
                "e7c7f72a8a5e93e4"
            ],
            [
                "e7c7f72a8a5e93e4"
            ]
        ]
    },
    {
        "id": "bf617086f0d43abc",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check deviceID04",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1450,
        "y": 700,
        "wires": []
    },
    {
        "id": "e7c7f72a8a5e93e4",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "setRequestForAllData",
        "func": "// @ts-nocheck\nvar msg ={\n    method:'GET',\n    url: 'https://data.weather.gov.hk/weatherAPI/smart-lamppost/smart-lamppost.php?pi=' + msg.payload.lamppost_id + '&di=' + msg.payload.device_id,\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1760,
        "y": 560,
        "wires": [
            [
                "a58ee1054e651ddb"
            ]
        ]
    },
    {
        "id": "a58ee1054e651ddb",
        "type": "http request",
        "z": "3c5de79e.539108",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1970,
        "y": 560,
        "wires": [
            [
                "c08569e42cc92766",
                "41d9300f3bf28ae6"
            ]
        ]
    },
    {
        "id": "c08569e42cc92766",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check get request",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2010,
        "y": 640,
        "wires": []
    },
    {
        "id": "41d9300f3bf28ae6",
        "type": "function",
        "z": "3c5de79e.539108",
        "name": "getPI",
        "func": "var lampInfo = { \n    payload: {\n        PI: msg.payload.PI,\n        humdity: msg.payload.BODY.HKO.RH,\n        // temp:msg.payload.BODY.HKO.TO\n    }\n};\nflow.set('lampInfo', lampInfo.payload);\n\nreturn lampInfo;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2150,
        "y": 560,
        "wires": [
            [
                "438bb9b3f164358c"
            ]
        ]
    },
    {
        "id": "438bb9b3f164358c",
        "type": "debug",
        "z": "3c5de79e.539108",
        "name": "check getInfo",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2280,
        "y": 620,
        "wires": []
    }
]

Hi I put the flow here, could you please check and help me? thank you

You have posted photos of two payloads which together give the latitude, longitude, temperature and humidity at Repulse Bay Beach.

After your join node, these will be combined into a single payload, and it ought to be a simple task to create a new message with all the elements.

However, I suspect that your real data may be much more complex, in particular it may contain values for multiple places, making things a little more complicated.

So I asked you to share some of your real data; you haven't.

My real data is get from the API, which is the real-time updated data.

I shared the flow with you, you can import the flow, and my real data is retrieved from the "HTTP request" node.

please kindly open the flow I just post :slight_smile:

I did that.

  1. The flow is disabled.
  2. It contains nodes which are not installed in my palette, so I cannot run it.
  3. If I take just the initial inject and HTTP request nodes I get this:

I did - and even installed the contrib-loop node. Unfortunately, your flow doesn't show the issue you have. There's a structure that looks similar...

... but it's not trying to perform the task you indicated.
I have an impression of what you try to achieve - yet to support further, there's a better starting point necessary...

You need to move the data from first request to preserve it for the end, if you check the end debug output all the info is there and can be manipulated at the end.

[{"id":"679763c87210febb","type":"inject","z":"3c5de79e.539108","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":240,"wires":[["fc28f3aa6bf7f897"]]},{"id":"fc28f3aa6bf7f897","type":"http request","z":"3c5de79e.539108","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://www.hko.gov.hk/common/hko_data/R3/files/smart_lamppost_met_device_location.json","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":290,"y":280,"wires":[["b9fa71faa1beab3c","66a9628cfafd3360"]]},{"id":"b9fa71faa1beab3c","type":"debug","z":"3c5de79e.539108","name":"check lamp type object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":400,"wires":[]},{"id":"66a9628cfafd3360","type":"split","z":"3c5de79e.539108","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":280,"wires":[["9bbe6f75b3d0e07c"]]},{"id":"9bbe6f75b3d0e07c","type":"function","z":"3c5de79e.539108","name":"getIDType","func":"msg.method = 'GET',\nmsg.url = 'https://data.weather.gov.hk/weatherAPI/smart-lamppost/smart-lamppost.php?pi=' + msg.payload.LP_NUMBER + '&di=' + msg.payload.LP_TYPE;\nmsg.lamp_post = msg.payload;\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":320,"wires":[["a58ee1054e651ddb"]]},{"id":"a58ee1054e651ddb","type":"http request","z":"3c5de79e.539108","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":810,"y":360,"wires":[["438bb9b3f164358c"]]},{"id":"438bb9b3f164358c","type":"debug","z":"3c5de79e.539108","name":"check getInfo","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":420,"wires":[]}]
2 Likes

thank you so much! it works!

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