JSON Merge

A very simple extension that merges two JSON objects. Objects can be submitted as text/string or JSON.
Just posting this because I saw someone needed that here and I just built something similar for Redis.

js:

exports.mergeJson = async function mergeJson(options) {
    options = this.parse(options);
    let json1 = options.json1;
    let json2 = options.json2;

    try {
        if (typeof json1 === 'string') {
            json1 = JSON.parse(json1);
        }
    } catch (error) {
        throw new Error('Failed to parse json1: ' + error.message);
    }

    try {
        if (typeof json2 === 'string') {
            json2 = JSON.parse(json2);
        }
    } catch (error) {
        throw new Error('Failed to parse json2: ' + error.message);
    }

    const mergedJson = {...json1, ...json2 };
    return mergedJson;
};

hjson:

{
    "type": "mergeJson",
    "module": "jsonActions",
    "action": "mergeJson",
    "groupTitle": "JSON Actions",
    "groupIcon": "fas fa-lg fa-code comp-data",
    "title": "Merge JSON",
    "icon": "fas fa-lg fa-code-merge comp-data",
    "dataPickObject": true,
    "properties": [
      {
        "group": "Properties",
        "variables": [
          {
            "name": "name",
            "optionName": "name",
            "title": "Name",
            "type": "text",
            "required": true,
            "defaultValue": ""
          },
          {
            "name": "output",
            "optionName": "output",
            "title": "Output",
            "type": "boolean",
            "defaultValue": false
          },
          {
            "name": "json1",
            "optionName": "json1",
            "title": "JSON 1",
            "type": "text",
            "required": true,
            "serverDataBindings": true,
            "defaultValue": ""
          },
          {
            "name": "json2",
            "optionName": "json2",
            "title": "JSON 2",
            "type": "text",
            "required": true,
            "serverDataBindings": true,
            "defaultValue": ""
          }
        ]
      }
    ]
}
3 Likes