Passing only certain values from a payload?

Alright guys, I feel like a knucklehead. Node-red has been running beautifully for awhile and I haven't needed to change anything. Now I need to extrapolate some data from an object and only pass those values. I thought I did it before with a Function Node, but I can't seem to get anything to work. This is the incoming data.

{"data":{"common":{"productId":{"wwwItemId":"276","upca":"630","productId":"7A9H","offerId":"EE212DDAE","ean13":"060312"},"name":"Litvst","department":"","thumbnailImageUrl":"","productImageUrl":"https://","customerRating":{"rating":4.1,"count":152},"productUrl":"https://","variantSwatches":[],"offerType":"ONLINE_AND_STORE","twoDayShippingEligible":false,"storePickupAvailable":false,"storePickupAvailableToday":false,"nextDayShippingEligible":false},"inStore":{"price":{"priceInCents":1000,"currencyUnit":"USD"},"location":{"aisles":["N9"],"detailedAisles":[{"zoneName":"N","aisleName":9,"sectionName":2}]},"storeId":XXXX},"online":{"price":{"priceInCents":2499,"currencyUnit":"USD","showInCart":false},"inventory":{"status":"In Stock","available":true},"specialOfferBadge":"rollback","specialOfferText":"Rollback"},"relatedItemsUrls":{"inStore":"https://","online":"https"}}

The values from the msg.payload is msg.payload.data.common.name, msg.payload.data.inStore.price.priceInCents, payload.data.inStore.storeId, etc.

I have a change node and function node which will extract msg.payload.data.common.name, but it becomes it's own separate message. I'd basically like to remove all the useless info from the payload. I just can't remember how.

[{"id":"822d599.c1c9ca8","type":"change","z":"c3e84d4e.4ead68","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.data.common.name","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":2160,"wires":[["1cb49f4.fe872e1","d847038d.95dde"]]},{"id":"1cb49f4.fe872e1","type":"function","z":"c3e84d4e.4ead68","name":"","func":"msg.payload = `Name:${msg.payload}`;\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":2160,"wires":[["3a4f0c57.7d8c74","a26ef6e1.a115d8"]]}]

There are many ways to skin a cat
e.g.

[{"id":"febf56d.68015a8","type":"inject","z":"6fcc3140.459a2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"common\":{\"productId\":{\"wwwItemId\":\"276\",\"upca\":\"630\",\"productId\":\"7A9H\",\"offerId\":\"EE212DDAE\",\"ean13\":\"060312\"},\"name\":\"Litvst\",\"department\":\"\",\"thumbnailImageUrl\":\"\",\"productImageUrl\":\"https://\",\"customerRating\":{\"rating\":4.1,\"count\":152},\"productUrl\":\"https://\",\"variantSwatches\":[],\"offerType\":\"ONLINE_AND_STORE\",\"twoDayShippingEligible\":false,\"storePickupAvailable\":false,\"storePickupAvailableToday\":false,\"nextDayShippingEligible\":false},\"inStore\":{\"price\":{\"priceInCents\":1000,\"currencyUnit\":\"USD\"},\"location\":{\"aisles\":[\"N9\"],\"detailedAisles\":[{\"zoneName\":\"N\",\"aisleName\":9,\"sectionName\":2}]},\"storeId\":1111},\"online\":{\"price\":{\"priceInCents\":2499,\"currencyUnit\":\"USD\",\"showInCart\":false},\"inventory\":{\"status\":\"In Stock\",\"available\":true},\"specialOfferBadge\":\"rollback\",\"specialOfferText\":\"Rollback\"},\"relatedItemsUrls\":{\"inStore\":\"https://\",\"online\":\"https\"}}}","payloadType":"json","x":220,"y":840,"wires":[["394060bf.ee1b3","97de1444.6039"]]},{"id":"394060bf.ee1b3","type":"change","z":"6fcc3140.459a2","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.data.common.name","pt":"msg","to":"hold.data.common.name","tot":"msg"},{"t":"set","p":"payload.data.inStore.price.priceInCents","pt":"msg","to":"hold.data.inStore.price.priceInCents","tot":"msg"},{"t":"set","p":"payload.data.inStore.storeId","pt":"msg","to":"hold.data.inStore.storeId","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":860,"wires":[["c7c1897c.129798"]]},{"id":"97de1444.6039","type":"debug","z":"6fcc3140.459a2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":660,"wires":[]}]

This certainly works! But I fail to understand why.

first it copies payload to hold
then it clears payload.
then it moves items from hold back to payload.

That's awesome! I had no idea you could do that.

Is there a way to make it spit out

name : TheNameOfSomething
price : priceInCents1000
storeId : XXX

That's Node-red's excellent low code interface. Respect Dev's

i would think so.
where are

name : TheNameOfSomething
price : priceInCents1000
storeId : XXX

located in your object?

And is that the string format or an object?

So if I use your example, I get

payload.data.common.name
payload.data.inStore.price.priceInCents
payload.data.inStore.storeId

in the debug node. I'm just curious if there's a way to make it look more clean (it'll be going into a spreadsheet hopefully.)

{{name : payload.data.common.name, price : payload.data.inStore.price.priceInCents, store : payload.data.inStore.storeId}}

(I have no idea what I'm doing. I can't remember how it worked.)

try

[{"id":"75500b3f.812ff4","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.name","pt":"msg","to":"hold.data.common.name","tot":"msg"},{"t":"set","p":"payload.price","pt":"msg","to":"{'priceInCents': hold.data.inStore.price.priceInCents} ","tot":"jsonata"},{"t":"set","p":"payload.storeId","pt":"msg","to":"hold.data.inStore.storeId","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":780,"wires":[["ca18810d.3ed83"]]}]

So close! The {'priceInCents': hold.data.inStore.price.priceInCents} comes up as it's own object and then you have to drill down to get the price.

In the debug node it shows {"name":"Litvst","price":OBJECT,"storeId":1111}

I did ask about your format, but you never answered. so from above i took the different colour of the 1000 as it was an object. How exactly do you wish the value in price to look?

Oh sorry about that, I must have missed it. I'm curling json data from a website. The 1000 is a variable that changes based on what I'm curling. So, I'm really not sure what it is. Thank you for your help. I'm terrible with node-red (javascript, variables, programming, function nodes, etc etc etc)

You never answer the question, I want the exact format you require. In future when asking for help it is best to give clear examples of input (your original json has an error , created by you when you edited in XXXX. a string with out quotes), and output, with clear examples, so people only have to do thing once. This is the third attempt, lets get it right this time.

I guess I'm trying to take

{"data":{"common":{"productId":{"wwwItemId":"276","upca":"630","productId":"7A9H","offerId":"EE212DDAE","ean13":"060312"},"name":"Litvst","department":"","thumbnailImageUrl":"","productImageUrl":"https://","customerRating":{"rating":4.1,"count":152},"productUrl":"https://","variantSwatches":[],"offerType":"ONLINE_AND_STORE","twoDayShippingEligible":false,"storePickupAvailable":false,"storePickupAvailableToday":false,"nextDayShippingEligible":false},"inStore":{"price":{"priceInCents":1099,"currencyUnit":"USD"},"location":{"aisles":["N9"],"detailedAisles":[{"zoneName":"N","aisleName":9,"sectionName":2}]},"storeId":2231},"online":{"price":{"priceInCents":2499,"currencyUnit":"USD","showInCart":false},"inventory":{"status":"In Stock","available":true},"specialOfferBadge":"rollback","specialOfferText":"Rollback"},"relatedItemsUrls":{"inStore":"https://","online":"https"}}

and have it display

"name":"Litvst"
"priceInCents":1099
"storeId":2231

Thats from the JSON I pulled. Those are the only values I need to extract from that long JSON. If it's an object, or string, or buffer, I really have no idea. All I know is the "Name" "priceInCents" and "storeId" values change depending what is being pulled.

I could curl another JSON and it coulf turn into

"name":"Tacos"
"priceInCents":100
"storeId":2771

ok let's hope.

[{"id":"75500b3f.812ff4","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"msg"},{"t":"set","p":"payload.name","pt":"msg","to":"hold.data.common.name","tot":"msg"},{"t":"set","p":"payload.priceInCents","pt":"msg","to":"hold.data.inStore.price.priceInCents","tot":"msg"},{"t":"set","p":"payload.storeId","pt":"msg","to":"hold.data.inStore.storeId","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":780,"wires":[["ca18810d.3ed83"]]}]

WOOHOO!! It worked!! Thank you so much!

just edited it to remove data property take another look at above post

Perfect!!!!

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