Rename Keys - Variable Keys

I have a workflow that processes data with a variable number of fields. The data returned is an array of line items with an array of properties inside it:

“line_items”:{
“id”:14097520066805,
“product_id”:null,
“properties”:[
{
“name”:“Exact Ownership or Lease Value”,
“value”:“10000”
},
{
“name”:“Main Prize Hole Number”,
“value”:“13”
},
{
“name”:“Tournament Date”,
“value”:“08/05/2023”
},
],
“quantity”:1,
“variant_id”:null,
“variant_title”:null,
}
}

I can use Rename Keys node to set the property names to their respective values. However, there are some optional values that do no get included in the array if they are unset on the order. When that happens, the Rename Keys node returns an error - the key doesn’t exist in those cases. Is there a way to loop through each of the properties values set each name in the array to its value?

Hey @Jeff_Hatfield,

Welcome to the community :raised_hands:

What are you expecting the data to look like once the node has finished? It could be that you need to use a code node to handle it so that you can deal with anything missing.

So I need to get the properties array to just be values in the line_item array. So it looks more like this:
“line_items”:{
“id”:14097520066805,
“product_id”:null,
“Exact Ownership or Lease Value”:“10000”
“Main Prize Hole Number”:“13”
“Tournament Date”:“08/05/2023”
“quantity”:1,
“variant_id”:null,
“variant_title”:null,
}
}

I can get that to work easy enough, but sometimes there are other name/value combinations in the properties array and I need to be able to handle those. I may have figured out a way, but it isn’t the cleanest - I can use IF expressions in a set node to check for the presence of the optional keys and if they aren’t present set them to a dummy value.

Thanks
Jeff

Hey @Jeff_Hatfield,

That is one way to do it, If you are going that far though maybe it is easier to use a code node so you can loop though the properties array and create new items from that, It can be done so that it doesn’t matter how many items are in the properties array by doing something like in the example below.

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