โ02-16-2023 06:29 AM
Hi Team,
I have my source data coming in JSON format. I want to sort the fields inside the JSON format alphabetically. One way is to use a mapper and arrange them one-one alphabetically but it is a tedious task if the fields are more. Can we use an expression in mapper or any other snap that does this for us?
Thank you.
Best Regards,
Darsh
Solved! Go to Solution.
โ02-16-2023 07:03 AM
Hi @darshthakkar,
One approach will be to Map the input document with the following expression:
$.entries().sort((a,b) => a[0].localeCompare(b[0])).reduce((acc,curr) => acc.extend({[curr[0]]:curr[1]}),{})
Note: This will only sort properties on first level.
Let me know if this helps.
BR,
Aleksandar.
โ02-16-2023 09:00 AM
Well, if the data that we have on input is in different format I might consider another approach because this is a special use case.
โ02-16-2023 09:36 AM
Makes sense, thank you @AleksandarAngelevski.
โ02-20-2023 07:03 AM
Alternative expression
$.keys().sort().toObject((k,i)=> k, (k,i)=> $.get(k))
โ02-20-2023 08:38 PM
Thank you so much @alchemiz for your suggestion.
Iโm sure it is going to work, will try and keep you postedโฆ
โ02-21-2023 07:30 AM
Hey @alchemiz,
The solution you provided worked, the only consideration is that - โYou need to be at the bottom of the hierarchy for sorting to workโ.
Thank you, anyways ๐