How to sort keys in a JSON Object

Table of Contents

  • Introduction
  • Use case

Introduction

There are some systems that require you to send a request with ordered keys in the payload. There may be some validations on the server side, which usually involves XSD (XML Schema Definition) or JSON Schema.

Alumio doesn’t save the order of the keys/properties from JSON object editors in the Alumio dashboard. Therefore, we provide you with a mapper called “Object: Sort Keys” that you can use to sort the keys/properties of a JSON object.

Use case

For example, you have a Value Setter that defines an object called data, with the value below.

{
  "id": 1,
  "name": "Product A",
  "sku": "ITEM-A",
  "qty": 99
}

When you save and reload the transformer page, Alumio doesn’t show the keys sorted the way you had defined before you saved the configuration. You can add the mapper “Object: sort keys” to the Value Setter using the sorting methods below.

  • Ascending: sorts the keys/properties in ascending order.

    Result:

    {
      "id": 1,
      "name": "Product A",
      "qty": 99,
      "sku": "ITEM-A"
    }
    
  • Descending: sorts the keys/properties in descending order.

    Result:

    {
      "sku": "ITEM-A"
      "qty": 99,
      "name": "Product A",
      "id": 1
    }
    
  • Manual: sort the keys/properties according to the defined order, such as below.

    Result:

    {
      "id": 1,
      "sku": "ITEM-A",
      "name": "Product A",
      "qty": 99
    }
    
1 Like