Ingest JSON to Function

I’ve been going around in circles trying to figure this out now for a few hours without any luck. I have a Javascript that is manipulating JSON data and I just need to bring it into the function in a way that is compatible. I have this:

const data = $items();

function getData(data,type) {
	const res = [];
  for(let row of data.results) {
    if(row.table._index == type) {
   	let kk = row.table._source.table;
      kk['id'] = row.table._id;
      res.push({json:kk});        
       }
  }
  return res;
}

return getData(data,'customers');

But that results in the error “ERROR: data.results is not iterable”. If I do this:

const data = $items();
return data;

I end up with the JSON from the previous HTTP Response. But obviously I’m missing something.

[
{
"quick_result": null,
"results": [
{
"table": {
"_id": 23462895,
"_type": "customer",
"_index": "customers",
"_source": {
"table": {
"firstname": "Benjamin",
"lastname": "Solo",
"email": "[email protected]",
"business_name": "Not so Clever Admin",
"phones": [
{
"id": 22570861,
"label": "Mobile",
"number": "2225556922",
"customer_id": 23462895,
"created_at": "2021-04-05T13:40:25.322-06:00",
"updated_at": "2021-04-05T13:40:25.322-06:00",
"extension": ""
}
]
}
}
}
},
{
"table": {
"_id": 23469053,
"_type": "customer",
"_index": "customers",
"_source": {
"table": {
"firstname": "Dan",
"lastname": "Solo",
"email": null,
"business_name": "Solo Killers",
"phones": [
{
"id": 22576437,
"label": "Mobile",
"number": "2225556922",
"customer_id": 23469053,
"created_at": "2021-04-05T22:24:07.258-06:00",
"updated_at": "2021-04-05T22:24:07.258-06:00",
"extension": ""
}
]
}
}
}
},
{
"table": {
"_id": 1339643,
"_type": "contact",
"_index": "contacts",
"_source": {
"table": {
"customer_id": 23462895,
"firstname": "John",
"lastname": "Solo",
"email": "",
"business_name": "Not so Clever Admin",
"phones": [
"2225556922",
""
]
}
}
}
},
{
"table": {
"_id": 1335594,
"_type": "contact",
"_index": "contacts",
"_source": {
"table": {
"customer_id": 23462918,
"firstname": "Right",
"lastname": "Guy",
"email": "",
"business_name": "Clever Admin",
"phones": [
"2225556922",
""
]
}
}
}
}
],
"error": null
}
]

Is that a function node or a function item node? So I understand that you want to filter just the ones with __type = customers given that input. Is that the case?

It’s a function node. That JSON is the result of a phone number search that provides a table of customers and a table of contacts. We are essentially looking to split that out into customers and contacts separately so that we can act on those results. If there is a single contact match, we grab the first one and do something with it, if there is no contact match, we do something with a single customer match, and if there are more than one customer matches, we do nothing.

Hope it helps:

const { json: { results } } = items[0];

const getData = (results, type) => results.filter(r => r.table._index === type).map(x => ({ id: x.table._id }));

return [ { json: { customers: getData(results, 'customers') } } ];
2 Likes