Creating an array from different named fields

Hi Makers

I am trying to process an array of applications, each one has details of people but annoyingly the field names from the source application are different for each person on the application. See simplified example…

{
“application”: [
{
“id”: 1,
“Person1FirstName”: “Alan”,
“Person1Surname”: “Atkins”,
“Person2FirstName”: “Bob”,
“Person2Surname”: “Brooks”,
“Person3FirstName”: “Charlie”,
“Person3Surname”: “Cooper”
},
{
“id”: 2,
“Person1FirstName”: “Amy”,
“Person1Surname”: “Adams”,
“Person2FirstName”: “Bella”,
“Person2Surname”: “Burton”,
“Person3FirstName”: “”,
“Person3Surname”: “”
}
]
}

I’d like to transform this to something like the below so that I can iterate through the applications and then iterate through all the people in the application and have the field names consistent.

Any help very gratefully received. Thanks

{
“application”: [
{
“id”: 1,
“person”: [
{
“FirstName”: “Alan”,
“Surname”: “Atkins”
},
{
“FirstName”: “Bob”,
“Surname”: “Brooks”
},
{
“FirstName”: “Charlie”,
“Surname”: “Cooper”
}
]
},
{
“id”: 2,
“person”: [
{
“FirstName”: “Amy”,
“Surname”: “Adams”
},
{
“FirstName”: “Bella”,
“Surname”: “Burton”
},
{
“FirstName”: “”,
“Surname”: “”
}
]
}
]
}

Welcome to the Make community!

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

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":182,"module":"util:ComposeTransformer","version":1,"parameters":{},"mapper":{"value":"{\n  \"application\": [\n    {\n      \"id\": 1,\n      \"Person1FirstName\": \"Alan\",\n      \"Person1Surname\": \"Atkins\",\n      \"Person2FirstName\": \"Bob\",\n      \"Person2Surname\": \"Brooks\",\n      \"Person3FirstName\": \"Charlie\",\n      \"Person3Surname\": \"Cooper\"\n    },\n    {\n      \"id\": 2,\n      \"Person1FirstName\": \"Amy\",\n      \"Person1Surname\": \"Adams\",\n      \"Person2FirstName\": \"Bella\",\n      \"Person2Surname\": \"Burton\",\n      \"Person3FirstName\": \"\",\n      \"Person3Surname\": \"\"\n    }\n  ]\n}"},"metadata":{"designer":{"x":65,"y":-2225,"name":"Transform to JSON"}}},{"id":180,"module":"regexp:Parser","version":1,"parameters":{"pattern":"(?<json>{\\s*\"id\": (?<id>\\d+),[^}]+})","global":true,"sensitive":true,"multiline":false,"singleline":false,"continueWhenNoRes":false,"ignoreInfiniteLoopsWhenGlobal":false},"mapper":{"text":"{{182.value}}"},"metadata":{"designer":{"x":311,"y":-2224},"parameters":[{"name":"pattern","type":"text","label":"Pattern","required":true},{"name":"global","type":"boolean","label":"Global match","required":true},{"name":"sensitive","type":"boolean","label":"Case sensitive","required":true},{"name":"multiline","type":"boolean","label":"Multiline","required":true},{"name":"singleline","type":"boolean","label":"Singleline","required":true},{"name":"continueWhenNoRes","type":"boolean","label":"Continue the execution of the route even if the module finds no matches","required":true},{"name":"ignoreInfiniteLoopsWhenGlobal","type":"boolean","label":"Ignore errors when there is an infinite search loop","required":true}]}},{"id":181,"module":"regexp:Parser","version":1,"parameters":{"pattern":"\"Person(?<num>\\d+)FirstName\": \"(?<FirstName>[^\"]+)\",\\s*\"Person\\1Surname\": \"(?<LastName>[^\"]+)\"","global":true,"sensitive":true,"multiline":false,"singleline":false,"continueWhenNoRes":false,"ignoreInfiniteLoopsWhenGlobal":false},"mapper":{"text":"{{180.json}}"},"metadata":{"designer":{"x":559,"y":-2223},"parameters":[{"name":"pattern","type":"text","label":"Pattern","required":true},{"name":"global","type":"boolean","label":"Global match","required":true},{"name":"sensitive","type":"boolean","label":"Case sensitive","required":true},{"name":"multiline","type":"boolean","label":"Multiline","required":true},{"name":"singleline","type":"boolean","label":"Singleline","required":true},{"name":"continueWhenNoRes","type":"boolean","label":"Continue the execution of the route even if the module finds no matches","required":true},{"name":"ignoreInfiniteLoopsWhenGlobal","type":"boolean","label":"Ignore errors when there is an infinite search loop","required":true}]}},{"id":183,"module":"builtin:BasicAggregator","version":1,"parameters":{"feeder":180},"mapper":{"FirstName":"{{181.FirstName}}","LastName":"{{181.LastName}}","id":"{{180.id}}"},"metadata":{"designer":{"x":859,"y":-2223,"messages":[{"category":"last","severity":"warning","message":"A transformer should not be the last module in the route."}]},"advanced":true},"flags":{"groupBy":"{{180.id}}"}}]}],"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.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like

Thank you so much - that’s really helpful. In my real world scenarios I have lots of attributes of an application outside of the details of people. I’d like to maintain this in an outer iteration and just be able to iterate through the people within this (see JSON below).

What nearly worked was using the add function to create an array of the different fields, but I couldn’t work out how to do this for multiple different fields, e.g. for first name and last name fields. Do you have any pointers?

Thanks again!

{
“application”: [
{
“id”: 1,
“initial payment”: 100,
“term”: 30,
“product”: “A”,
“Person1FirstName”: “Alan”,
“Person1Surname”: “Atkins”,
“Person2FirstName”: “Bob”,
“Person2Surname”: “Brooks”,
“Person3FirstName”: “Charlie”,
“Person3Surname”: “Cooper”
},
{
“id”: 2,
“initial payment”: 200,
“term”: 24,
“product”: “B”,
“Person1FirstName”: “Amy”,
“Person1Surname”: “Adams”,
“Person2FirstName”: “Bella”,
“Person2Surname”: “Burton”,
“Person3FirstName”: “”,
“Person3Surname”: “”
}
]
}

Since the requirements have changed, please start a new topic for this, as I am currently busy and cannot continue this thread.

So what I ended up doing was creating an array of members using set variable

I then created a data structure from this that I could iterate through. I’m not sure if this is the best way to do this but it has worked for me so I though I would share it. Thanks for the help.

3 Likes

Yes, whatever works for you is the best way.

You could move the “variable value” field contents directly into the Parse JSON module, saving an operation.

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

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like