Forum Discussion

darshthakkar's avatar
darshthakkar
Valued Contributor
3 years ago
Solved

Sort field names alphabetically in JSON source data

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

  • 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.

  • As we cannot mark multiple suggestions as “Solutions”, wanted to summarize the solution that will work for this use case:

    1. $.entries().sort((a,b) => a[0].localeCompare(b[0])).reduce((acc,curr) => acc.extend({[curr[0]]:curr[1]}),{})

    2. $.keys().sort().toObject((k,i)=> k, (k,i)=> $.get(k))

    Both the expressions mentioned above will ONLY work if we are at the last stage of the hierarchy.

    How I broke the hierarchy: Using JSON Splitter to reorganise JSON - #3 by Supratim

    Thank you @AleksandarAngelevski , @alchemiz and @Supratim for your inputs. Closing this thread now.

12 Replies

  • 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.

  • darshthakkar's avatar
    darshthakkar
    Valued Contributor

    As we cannot mark multiple suggestions as “Solutions”, wanted to summarize the solution that will work for this use case:

    1. $.entries().sort((a,b) => a[0].localeCompare(b[0])).reduce((acc,curr) => acc.extend({[curr[0]]:curr[1]}),{})

    2. $.keys().sort().toObject((k,i)=> k, (k,i)=> $.get(k))

    Both the expressions mentioned above will ONLY work if we are at the last stage of the hierarchy.

    How I broke the hierarchy: Using JSON Splitter to reorganise JSON - #3 by Supratim

    Thank you @AleksandarAngelevski , @alchemiz and @Supratim for your inputs. Closing this thread now.

    • alchemiz's avatar
      alchemiz
      Contributor III

      $.keys().sort().reverse().toObject((k,i)=> k, (k,i)=> $.get(k))

  • darshthakkar's avatar
    darshthakkar
    Valued Contributor

    @AleksandarAngelevski - Is it a safe assumption that we can use the same expression with a minor tweak to organize data alphabetically even if it’s not the JSON format?

  • darshthakkar's avatar
    darshthakkar
    Valued Contributor

    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 🙂