Dataweave Code to Sort Nested JSON Objects & Arrays

This document covers Dataweave code to sort nested JSON objects and arrays. For this demonstration, we are using Mulesoft's Online Dataweave Playground - Link as an IDE.

Let’s begin...

Solution Preview

Dataweave Code to Sort Nested JSON Objects & Arrays

Code Snippet

%dw 2.0
fun sortObjectAndArray(inputPayload) = (
  if(inputPayload is Object) (
    inputPayload mapObject ((value, key, index) -> 
      (key): sortObjectAndArray(value)
    ) orderBy $$
  )
  else if(inputPayload is Array) (
    if(inputPayload[0] is Object)
      inputPayload map ((item, index) -> 
        sortObjectAndArray(item)
      )
    else
      inputPayload orderBy $
  )
  else inputPayload
)
output application/json
---
sortObjectAndArray(payload)

Brief Explanation

This code traverses through a nested JSON Object or Array input and sorts it. Here we have a user-defined function named sortObjectAndArray, which calls itself (recursively) every time it detects a nested Array or Object.

For Object, the key-value pairs are sorted in ascending order of the keys.

For Array, the elements are sorted in ascending order of the values.

For JSON Array, the individual object's key-value pairs are sorted, but the object as a whole remains in the same position as the input.

In my opinion, this code is useful in the Experience API layer to beautify, streamline, and make the output payload more coherent with the rest of the systems.

Sample Input

{
  "name": "Alice Smith",
  "age": 25,
  "is_student": true,
  "additional_data": {
    "data_array": [
      {"key2": "value2", "key1": "value1"},
      {"key1": "value3", "key2": "value4"}
    ]
  },
  "hobbies": ["Hiking", "Reading", "Swimming", "Cooking"],
  "skills": [
    {"type": "Programming", "languages": ["C#", "Java", "Python", "JavaScript"]},
    {"type": "Design", "tools": ["Sketch", "Figma", "Illustrator", "Photoshop"]},
    {"type": "Writing", "styles": ["Creative", "Copyediting", "Technical Writing", "Blogging"]}
  ],
  "emails": ["alice@example.com", "asmith@gmail.com", "alice.smith@company.com"],
  "address": {
    "street": "456 Elm St",
    "country": "Canada",
    "city": "Smalltown"
  },
  "phones": ["555-123-4567", "555-987-6543", "555-789-0123"]
}

Output

{
  "additional_data": {
    "data_array": [
      {
        "key1": "value1",
        "key2": "value2"
      },
      {
        "key1": "value3",
        "key2": "value4"
      }
    ]
  },
  "address": {
    "city": "Smalltown",
    "country": "Canada",
    "street": "456 Elm St"
  },
  "age": 25,
  "emails": [
    "alice.smith@company.com",
    "alice@example.com",
    "asmith@gmail.com"
  ],
  "hobbies": [
    "Cooking",
    "Hiking",
    "Reading",
    "Swimming"
  ],
  "is_student": true,
  "name": "Alice Smith",
  "phones": [
    "555-123-4567",
    "555-789-0123",
    "555-987-6543"
  ],
  "skills": [
    {
      "languages": [
        "C#",
        "Java",
        "JavaScript",
        "Python"
      ],
      "type": "Programming"
    },
    {
      "tools": [
        "Figma",
        "Illustrator",
        "Photoshop",
        "Sketch"
      ],
      "type": "Design"
    },
    {
      "styles": [
        "Blogging",
        "Copyediting",
        "Creative",
        "Technical Writing"
      ],
      "type": "Writing"
    }
  ]
}

Comments