Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add JSONata node #2556

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/nodes-base/nodes/JSONata/JSONata.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"node": "n8n-nodes-base.jsonAta",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": [
"Core Nodes"
],
"resources": {
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/nodes/n8n-nodes-base.jsonAta/"
}
]
},
"alias": [
"Query"
],
"subcategories": {
"Core Nodes": [
"Helpers"
]
}
}
93 changes: 93 additions & 0 deletions packages/nodes-base/nodes/JSONata/JSONata.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';

import { set } from 'lodash';
import jsonata from 'jsonata';
lublak marked this conversation as resolved.
Show resolved Hide resolved

export class JSONata implements INodeType {
description: INodeTypeDescription = {
displayName: 'JSONata',
name: 'jsonata',
group: ['transform'],
version: 1,
description: 'Use the JSON query and transformation language.',
defaults: {
name: 'JSONata',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Query',
name: 'query',
type: 'string',
default: '',
placeholder: '*',
required: true,
description: 'The JSONata query.',
},
{
displayName: 'Destination Key',
name: 'destinationKey',
type: 'string',
default: 'data',
required: true,
placeholder: 'data',
description: 'The name the JSON key to copy data to. It is also possible<br />to define deep keys by using dot-notation like for example:<br />"level1.level2.newKey"',
},
{
displayName: 'Complete',
name: 'complete',
type: 'boolean',
default: true,
description: 'Use complete input as json data. Not each entry independently.',
},
],
};


async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const complete = this.getNodeParameter('complete', 0) as boolean;

if(complete) {
const destinationKey = this.getNodeParameter('destinationKey', 0) as string;
const query = this.getNodeParameter('query', 0) as string;
const data: IDataObject = {};
set(data, destinationKey, jsonata(query).evaluate(items.map(item => item.json)));
return this.prepareOutputData([{json: data}]);
} else {
let destinationKey;
let query;
let item;
let newItem: INodeExecutionData;
const returnData: INodeExecutionData[] = [];

for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
query = this.getNodeParameter('query', itemIndex) as string;
destinationKey = this.getNodeParameter('destinationKey', itemIndex) as string;

item = items[itemIndex];

newItem = {
json: JSON.parse(JSON.stringify(item.json)),
};

if(item.binary !== undefined) {
newItem.binary = {};
Object.assign(newItem.binary, item.binary);
}

set(newItem.json, destinationKey, jsonata(query).evaluate(item.json));
returnData.push(newItem);
}
return this.prepareOutputData(returnData);
}
}
}
1 change: 1 addition & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@
"ics": "^2.27.0",
"imap-simple": "^4.3.0",
"iso-639-1": "^2.1.3",
"jsonata": "^1.8.5",
"jsonwebtoken": "^8.5.1",
"kafkajs": "^1.14.0",
"lodash.get": "^4.4.2",
Expand Down