Merging two JSON

I’m trying to merge two data sources into one JSON, similar to how you would with SQL. I cannot figure out how to do that with the modules. The location is the mapping key.

Input JSON 1:

{
    "location_details": [
        {
            "email": "lukas.mueller@example.com",
            "first_name": "Lukas",
            "last_name": "Müller",
            "location": "A"
        },
        {
            "email": "anna.schmidt@example.com",
            "first_name": "Anna",
            "last_name": "Schmidt",
            "location": "B"
        },
        {
            "email": "johannes.fischer@example.com",
            "first_name": "Johannes",
            "last_name": "Fischer",
            "location": "C"
        }
    ]
}

Input JSON 2:

{
    "product_report": [
        {
            "product_id": 267375,
            "availability ": 3,
            "description": "Description Product 267375",
            "location": "A"
        },
        {
            "product_id": 45567,
            "availability ": 7,
            "description": "Description Product 45567",
            "location": "A"
        },
        {
            "product_id": 89075,
            "availability ": 123,
            "description": "Description Product 89075",
            "location": "A"
        },
        {
            "product_id": 22345,
            "availability ": 45,
            "description": "Description Product 22345",
            "location": "B"
        },
        {
            "product_id": 567990,
            "availability ": 56,
            "description": "Description Product 567990",
            "location": "B"
        },
        {
            "product_id": 332345,
            "availability ": 89,
            "description": "Description Product 332345",
            "location": "C"
        }
    ]
}

Expected Result:

[
    {
        "email": "lukas.mueller@example.com",
        "first_name": "Lukas",
        "last_name": "Müller",
        "location": "A",
        "products": [
            {
                "product_id": 267375,
                "availability ": 3,
                "description": "Description Product 267375",
                "location": "A"
            },
            {
                "product_id": 45567,
                "availability ": 7,
                "description": "Description Product 45567",
                "location": "A"
            },
            {
                "product_id": 89075,
                "availability ": 123,
                "description": "Description Product 89075",
                "location": "A"
            }
        ]
    },
    {
        "email": "anna.schmidt@example.com",
        "first_name": "Anna",
        "last_name": "Schmidt",
        "location": "B",
        "products": [
            {
                "product_id": 22345,
                "availability ": 45,
                "description": "Description Product 22345",
                "location": "B"
            },
            {
                "product_id": 567990,
                "availability ": 56,
                "description": "Description Product 567990",
                "location": "B"
            }
        ]
    },
    {
        "email": "johannes.fischer@example.com",
        "first_name": "Johannes",
        "last_name": "Fischer",
        "location": "C",
        "products": [
            {
                "product_id": 332345,
                "availability ": 89,
                "description": "Description Product 332345",
                "location": "C"
            }
        ]
    }
]

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of five modules:

Output

This is just an example. Your final solution may or may not look like this depending on your requirements.

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) shown in my screenshots above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":309,"module":"json:ParseJSON","version":1,"parameters":{"type":""},"mapper":{"json":"{\"location_details\":[{\"email\":\"lukas.mueller@example.com\",\"first_name\":\"Lukas\",\"last_name\":\"Müller\",\"location\":\"A\"},{\"email\":\"anna.schmidt@example.com\",\"first_name\":\"Anna\",\"last_name\":\"Schmidt\",\"location\":\"B\"},{\"email\":\"johannes.fischer@example.com\",\"first_name\":\"Johannes\",\"last_name\":\"Fischer\",\"location\":\"C\"}]}"},"metadata":{"designer":{"x":1513,"y":-2394,"name":"Locations"},"parameters":[{"name":"type","type":"udt","label":"Data structure"}]}},{"id":307,"module":"json:ParseJSON","version":1,"parameters":{"type":""},"mapper":{"json":"{\"product_report\":[{\"product_id\":267375,\"availability \":3,\"description\":\"Description Product 267375\",\"location\":\"A\"},{\"product_id\":45567,\"availability \":7,\"description\":\"Description Product 45567\",\"location\":\"A\"},{\"product_id\":89075,\"availability \":123,\"description\":\"Description Product 89075\",\"location\":\"A\"},{\"product_id\":22345,\"availability \":45,\"description\":\"Description Product 22345\",\"location\":\"B\"},{\"product_id\":567990,\"availability \":56,\"description\":\"Description Product 567990\",\"location\":\"B\"},{\"product_id\":332345,\"availability \":89,\"description\":\"Description Product 332345\",\"location\":\"C\"}]}"},"metadata":{"designer":{"x":1766,"y":-2395,"name":"Products"},"parameters":[{"name":"type","type":"udt","label":"Data structure"}]}},{"id":311,"module":"builtin:BasicFeeder","version":1,"parameters":{},"mapper":{"array":"{{309.location_details}}"},"metadata":{"designer":{"x":2013,"y":-2388,"name":"Iterate Locations"}}},{"id":308,"module":"builtin:BasicFeeder","version":1,"parameters":{},"mapper":{"array":"{{307.product_report}}"},"metadata":{"designer":{"x":2262,"y":-2388,"name":"Iterate Products"}}},{"id":315,"module":"util:TextAggregator","version":1,"parameters":{"rowSeparator":"other","otherRowSeparator":",","feeder":308},"filter":{"name":"current location","conditions":[[{"a":"{{308.location}}","o":"text:equal","b":"{{311.location}}"}]]},"mapper":{"value":"{\n  \"product_id\": {{308.product_id}},\n  \"availability \": {{308.`availability `}},\n  \"description\": \"{{308.description}}\",\n  \"location\": \"{{308.location}}\"\n}"},"metadata":{"designer":{"x":2599,"y":-2389,"name":"Aggregate Products"},"parameters":[{"name":"rowSeparator","type":"select","label":"Row separator","validate":{"enum":["\n","\t","other"]}},{"name":"otherRowSeparator","type":"text","label":"Separator"}],"advanced":true},"flags":{"stopIfEmpty":true}},{"id":316,"module":"util:TextAggregator","version":1,"parameters":{"rowSeparator":"other","otherRowSeparator":",","feeder":311},"mapper":{"value":"{\n  \"email\": \"{{311.email}}\",\n  \"first_name\": \"{{311.first_name}}\",\n  \"last_name\": \"{{311.last_name}}\",\n  \"location\": \"{{311.location}}\",\n  \"products\": [{{315.text}}]\n}"},"metadata":{"designer":{"x":2846,"y":-2390,"name":"Aggregate Products"},"parameters":[{"name":"rowSeparator","type":"select","label":"Row separator","validate":{"enum":["\n","\t","other"]}},{"name":"otherRowSeparator","type":"text","label":"Separator"}],"advanced":true},"flags":{"stopIfEmpty":true}},{"id":317,"module":"json:ParseJSON","version":1,"parameters":{"type":""},"mapper":{"json":"[{{316.text}}]"},"metadata":{"designer":{"x":3093,"y":-2392},"parameters":[{"name":"type","type":"udt","label":"Data structure"}]}}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

2 Likes

Excellent! I was missing the concept of the text aggregator and the location filter! It works like a charm!
Thanks for your help, especially for the example that’s ready to go by just pasting it into the scenario. Really cool feature, by the way.