How to input a same property into an array

Hello, Im a newbie here, I have a question with my sample json file(payload) in the image below. I want to input a property WindSpeed into the payload.Weather. I could do it manually like using a change node and set payload.Weather[0].WindSpeed upto the index[4]. But how can i put simultaneously? Thanks in advance for the help.

This is my desired result. Windspeed below the "Day" value.

If you want to do it to the whole array then you can so it in a Function node using Array.forEach(). Something like

msg.payload.Weather.forEach(element => element.WindSpeed = "3.5");
return msg

If you just want to do it on a part of the array then

for (let i = 0; i <= 4; i++) {
  msg.payload.Weather[i] = "3.5"
}
return msg
1 Like

To do so in a change node with JSONata
$$.payload ~> |Weather|{"WindSpeed": "3.5"}|
"3.5" can be a msg property.
[edit] for future readers, to delete the property Today it would be
$$.payload ~> |Weather|{},"Today"|
Where the array "Today" can be an array of proerty names. You can add and delete at same time.
$$.payload ~> |Weather|{"WindSpeed": "3.5"}, "Today"|

1 Like

Thanks for your answers. I will try.

Hi @E1cid thanks for your answer I was able to input property in the array. However Is it possible to input a property from a different msg.property? Like in this input json file

{
	"Weather": [
		{
			"Day": "Monday",
			"Today": "Nice",
			"Tomorrow": "ebasdva",
			"Nextweek": "sdvaerg"
		},
		{
			"Day": "Tuesday",
			"Today": "Good",
			"Tomorrow": "raersebeb",
			"Nextweek": ""
		},
		{
			"Day": "Wednesday",
			"Today": "Very Good",
			"Tomorrow": "argarwg",
			"Nextweek": "awergawgr"
		},
		{
			"Day": "Thursday",
			"Today": "Nice",
			"Tomorrow": "argarg",
			"Nextweek": ""
		},
		{
			"Day": "Friday",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}
	],
	"WeekNumber": [
		{
			"day": "Monday",
			"DayNumber": "01"
		},
		{
			"day": "Tuesday",
			"DayNumber": "02"
		},
		{
			"day": "Wednesday",
			"DayNumber": "03"
		},
		{
			"day": "Thursday",
			"DayNumber": "04"
		},
		{
			"day": "Friday",
			"DayNumber": "05"
		}
	]
}

If the property Weather.Day matches with WeekNumber.day, it will input the data of WeekNumber.day to a new property Weather.DayNumber. Like in the json below which is my desired outcome.

{
	"Weather": [
		{
			"Day": "Monday",
			"DayNumber": "01",
			"Today": "Nice",
			"Tomorrow": "ebasdva",
			"Nextweek": "sdvaerg"
		},
		{
			"Day": "Tuesday",
			"DayNumber": "02",
			"Today": "Good",
			"Tomorrow": "raersebeb",
			"Nextweek": ""
		},
		{
			"Day": "Wednesday",
			"DayNumber": "03",
			"Today": "Very Good",
			"Tomorrow": "argarwg",
			"Nextweek": "awergawgr"
		},
		{
			"Day": "Thursday",
			"DayNumber": "04",
			"Today": "Nice",
			"Tomorrow": "argarg",
			"Nextweek": ""
		},
		{
			"Day": "Friday",
			"DayNumber": "05",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}
	]
	
}

Thanks in advance for your reply. I am trying to learn.

Is weeknumber array generated by you or returned from some api?
Also is weather array in msg or payload?
Do you simply want a daynumber in all objects that matches the Day?

Best to supply an example flow with data set in inject nodes to simulate what is actually happening, other wise we are left to fill in the gaps, which is not always correct.

WeekNumber is an array from an API. Weather is from payload. Yes exactly, I want the daynumber in all objects that matches the day.

Try

(
  $daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber};
  $$.payload ~> |$.Weather|{"DayNumber": $lookup($daylookup,$.Day)}|
)

The first line creates a look up object.

{
  "Monday":  "01",
  "Tuesday": "02",
  etc.......
}

The second line adds the property using the lookup object

Flow example

[{"id":"05232c7e552429bd","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Weather\":[{\"Day\":\"Monday\",\"Today\":\"Nice\",\"Tomorrow\":\"ebasdva\",\"Nextweek\":\"sdvaerg\"},{\"Day\":\"Tuesday\",\"Today\":\"Good\",\"Tomorrow\":\"raersebeb\",\"Nextweek\":\"\"},{\"Day\":\"Wednesday\",\"Today\":\"Very Good\",\"Tomorrow\":\"argarwg\",\"Nextweek\":\"awergawgr\"},{\"Day\":\"Thursday\",\"Today\":\"Nice\",\"Tomorrow\":\"argarg\",\"Nextweek\":\"\"},{\"Day\":\"Friday\",\"Today\":\"eweffw34\",\"Tomorrow\":\"32fevd\",\"Nextweek\":\"32feb\"}],\"WeekNumber\":[{\"day\":\"Monday\",\"DayNumber\":\"01\"},{\"day\":\"Tuesday\",\"DayNumber\":\"02\"},{\"day\":\"Wednesday\",\"DayNumber\":\"03\"},{\"day\":\"Thursday\",\"DayNumber\":\"04\"},{\"day\":\"Friday\",\"DayNumber\":\"05\"}]}","payloadType":"json","x":330,"y":4400,"wires":[["e1adeee7fae3b8ed"]]},{"id":"e1adeee7fae3b8ed","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t  $daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber};\t  $$.payload ~> |$.Weather|{\"DayNumber\": $lookup($daylookup,$.Day)}|\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":4400,"wires":[["17f95f5b251d8b43"]]},{"id":"17f95f5b251d8b43","type":"debug","z":"65617ffeb779f51c","name":"debug 250","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4400,"wires":[]}]

Thanks a lot @E1cid it worked on my flow. But i have one last question,
the result is like this

{
			"Day": "Friday",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
			"DayNumber": "05"
		}

how can i make it

{
			"Day": "Friday",
			"DayNumber": "05",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}

The DayNumber goes on the 2nd line.

The order of objects do not matter, please explain why you want that order. As objects are orderless. It could be done, but seems pointless.

Because the output requires a format. I know in json it does not matter, but it is a response to a client API and needs a proper format :pray:

And if you could lead me to proper resources where I can study more of it. It seems the documantation in Jsonata.org does not have this things.

I doubt that the object order will affect the api, have you tried it as is?

Please define things does it mean the transform syntax |...|...|if so Other Operators · JSONata
Or parenthesized expressions blocks (...; ...)Query composition · JSONata
Or variable binding := Other Operators · JSONata

Thanks for the links I haven't got thru them yet. I will study more.

I doubt that the object order will affect the api, have you tried it as is?

Yes i have tried as it is and i got a comment to redo the order of the objects.

Could you you provide the error received

Here is how you will have to do it to maintain the order

[{"id":"bacc35bea7d25bee","type":"inject","z":"8be23377afc3327e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \t\"Weather\": [ \t\t{ \t\t\t\"Day\": \"Monday\", \t\t\t\"Today\": \"Nice\", \t\t\t\"Tomorrow\": \"ebasdva\", \t\t\t\"Nextweek\": \"sdvaerg\" \t\t}, \t\t{ \t\t\t\"Day\": \"Tuesday\", \t\t\t\"Today\": \"Good\", \t\t\t\"Tomorrow\": \"raersebeb\", \t\t\t\"Nextweek\": \"\" \t\t}, \t\t{ \t\t\t\"Day\": \"Wednesday\", \t\t\t\"Today\": \"Very Good\", \t\t\t\"Tomorrow\": \"argarwg\", \t\t\t\"Nextweek\": \"awergawgr\" \t\t}, \t\t{ \t\t\t\"Day\": \"Thursday\", \t\t\t\"Today\": \"Nice\", \t\t\t\"Tomorrow\": \"argarg\", \t\t\t\"Nextweek\": \"\" \t\t}, \t\t{ \t\t\t\"Day\": \"Friday\", \t\t\t\"Today\": \"eweffw34\", \t\t\t\"Tomorrow\": \"32fevd\", \t\t\t\"Nextweek\": \"32feb\" \t\t} \t], \t\"WeekNumber\": [ \t\t{ \t\t\t\"day\": \"Monday\", \t\t\t\"DayNumber\": \"01\" \t\t}, \t\t{ \t\t\t\"day\": \"Tuesday\", \t\t\t\"DayNumber\": \"02\" \t\t}, \t\t{ \t\t\t\"day\": \"Wednesday\", \t\t\t\"DayNumber\": \"03\" \t\t}, \t\t{ \t\t\t\"day\": \"Thursday\", \t\t\t\"DayNumber\": \"04\" \t\t}, \t\t{ \t\t\t\"day\": \"Friday\", \t\t\t\"DayNumber\": \"05\" \t\t} \t] }","payloadType":"json","x":390,"y":320,"wires":[["cbd8285fc385a66c"]]},{"id":"cbd8285fc385a66c","type":"change","z":"8be23377afc3327e","name":"","rules":[{"t":"set","p":"payload.Weather","pt":"msg","to":"(   \t$daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber};   \t$$.payload.Weather.{\t  \"Day\": $.Day,\t  \"DayNumber\":$lookup($daylookup, $.Day),\t  \"Today\": $.Today,\t  \"Tomorrow\": $.Tomorrow,\t  \"Nextweek\": $.Nextweek\t}\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":280,"wires":[["a0b3732a8e570fe9"]]},{"id":"a0b3732a8e570fe9","type":"debug","z":"8be23377afc3327e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":320,"wires":[]}]
1 Like

It is not an error but a verbal comment from the client to fix the order of the objects.
I will try your code. Thank You so much!

Then tell the client that objects have no order and even if we put it in an order, it is not guaranteed. And their instruction is like asking you to use a sieve to carry water. A waste of time.

The only way to do this would be to use a Map Object - plus some JavaScript tinkering. But as E1cid says, insisting on having an Objects properties in a particular order is daft.

or checkout javascript - Changing the order of the Object keys.... - Stack Overflow

1 Like