JSON transform HELP

Hi!

How to display values as an array:

“tax_input-any 1”: [“value 1”, “value 2”],
“tax_input-any 2”: [“value 1”, “value 2”],
“tax_input-any 3”: [“value 1”, “value 2”]

And leave everything “meta_input - ***” as is.

Original code from Function node:

const result = [];
for (const item of items) {
  const json = item.json;
  let taxInputs = {};
  let metaInputs = {};
  for (const key of Object.keys(json)) {
if (key.includes('tax_input') && key !== 'tax_input') {
    taxInputs[key] = json[key];
}
  if (key.includes('meta_input') && key !== 'meta_input') {
    metaInputs[key] = json[key];
}
  }
  result.push({
json: {
    action: "create_post",
    post_title: json['post_title'],
    post_status: json['post_status'],
    post_type: json['post_type'],
    tax_input: taxInputs,
    meta_input: metaInputs,
}
  })
  taxInputs = [];
  metaInputs = {};
  return result
}

Sorry, do not understand. Looking at the code it seems like there can never be multiple values for any of the “tax_input-any 1”, …
So the only thing you want to be changed is that the single string-value that gets set right now becomes an array instead?
If that is correct you would simply change this line:

taxInputs[key] = json[key];

to:

taxInputs[key] = [json[key]];

Thanks, it works, but now you need to make a “,” as a separator for values?

In this case you have to split the values. So something like this:

taxInputs[key] = json[key].split(',').map(item => item.trim()).filter(item => !!item);
1 Like

Thanks Jan again!