How to Sort and Filter JSON Data
Tags: webdev, api, productivity, json
Published: 2023-12-31 22:50:14

## 1. Introduction

JSON (JavaScript Object Notation) is a popular data format used for exchanging and storing information. As JSON data gets more complex, filtering and sorting become important tools for quickly extracting relevant information and organizing data. Filtering helps to reduce processing time and resource consumption by removing unwanted data, while sorting makes it easier to identify patterns and relationships, enabling better analysis and decision-making. In this article, we'll explore how to use a free API to filter and sort JSON data easily. You can find the link to the API [here](https://rapidapi.com/afifii/api/json-sort-and-filter).

## 2. What is JSON Orientation

There are different JSON orientations that you can use with this API, the orientation is the way the data is structured in JSON. This API supports five orientations: {'split', 'records', 'index', 'columns', 'values'}. Below is a list summarizing the differences between them:

- 'split' : dict like {index -> [index], columns -> [columns], data -> [values]}
- 'records' : list like [{column -> value}, ... , {column -> value}]
- 'index' : dict like {index -> {column -> value}}
- 'columns' : dict like {column -> {index -> value}}
- 'values' : just the values array

The API can interpret some of the common orientations, however, if you face any issues, it is better if you try adding the orientation first.

## 3. Sorting

```
/sort
```

First, let's explore how to use it to sort JSON. We will explore some examples below of requests and their respective responses to show how the API works.

### 3.1. Sort Text Ascending

Request:
```json
{
    "data": [
        {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    ],
    "json_orient": "records",
    "field": "name"
}
```

Response:
```json
[
  {
    "name": "Bombom",
    "age": 27,
    "city": "Cairo"
  },
  {
    "name": "John",
    "age": 31,
    "city": "New York"
  },
  {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  }
]
```

###3.2. Text Ascending With No Orientation

Request:
```json
{
    "data": [
        {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    ],
    "field": "name"
}
```

Response:
```json
[
  {
    "name": "Bombom",
    "age": 27,
    "city": "Cairo"
  },
  {
    "name": "John",
    "age": 31,
    "city": "New York"
  },
  {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  }
]
```

###3.3. Text Descending

Request:
```json
{
    "data": {
        "0": {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        "1": {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        "2": {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        "3": {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    },
    "field": "name",
    "ascending": false
}
```

Response:
```json
[
    {
        "name": "Omar",
        "age": 28,
        "city": "Cairo"
    },
    {
        "name": "Mark",
        "age": 28,
        "city": "Cairo"
    },
    {
        "name": "John",
        "age": 31,
        "city": "New York"
    },
    {
        "name": "Bombom",
        "age": 27,
        "city": "Cairo"
    }
]
```

### 3.4. Sorting Numbers

Request:
```json
{
  "data": [
      {"name":"John", "age":31, "city":"New York"},
      {"name":"Omar", "age":28, "city":"Cairo"},
      {"name":"Mark", "age":28, "city":"Cairo"},
      {"name":"Ahmed", "age":27, "city":"Cairo"}
  ],
  "json_orient": "records",
  "field": "age"
}
```

Response:
```json
[
    {
        "name": "Ahmed",
        "age": 27,
        "city": "Cairo"
    },
    {
        "name": "Omar",
        "age": 28,
        "city": "Cairo"
    },
    {
        "name": "Mark",
        "age": 28,
        "city": "Cairo"
    },
    {
        "name": "John",
        "age": 31,
        "city": "New York"
    }
]
```

## 4. Filtering

```
/filter
```

Now, let's explore how to filter JSON using the API.

###4.1. Example 1

Filtering by a number.

Request:
```json
{
    "data": [
        {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    ],
    "json_orient": "records",
    "field": "age",
    "field_filter": "28"
}
```

Response:
```json
[
  {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  }
]
```
###4.2. Example 2

Filter by number with JSON orientation not specified.

Request:
```json
{
    "data": [
        {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    ],
    "field": "age",
    "field_filter": "28"
}
```

Response:
```json
[
  {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  }
]
```

###Example 3

Filtering by text.

Request:
```json
{
    "data": [
        {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    ],
    "field": "city",
    "field_filter": "Cairo"
}
```

Response:
```json
[
  {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  },
  {
    "name": "Bombom",
    "age": 27,
    "city": "Cairo"
  }
]
```

###Example 4

Filtering data in the index JSON orientation by text without specifying the orientation.

Request:
```json
{
    "data": {
        "0": {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        "1": {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        "2": {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        "3": {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    },
    "field": "city",
    "field_filter": "Cairo"
}
```

Response:
```json
{
  "1": {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  },
  "2": {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  },
  "3": {
    "name": "Bombom",
    "age": 27,
    "city": "Cairo"
  }
}
```

###Example 5

Filtering data in the index JSON orientation and specifying the orientation.

Request:
```json
{
    "data": {
        "0": {
            "name": "John",
            "age": 31,
            "city": "New York"
        },
        "1": {
            "name": "Omar",
            "age": 28,
            "city": "Cairo"
        },
        "2": {
            "name": "Mark",
            "age": 28,
            "city": "Cairo"
        },
        "3": {
            "name": "Bombom",
            "age": 27,
            "city": "Cairo"
        }
    },
    "json_orient": "index",
    "field": "city",
    "field_filter": "Cairo"
}
```

Response:
```json
{
  "1": {
    "name": "Omar",
    "age": 28,
    "city": "Cairo"
  },
  "2": {
    "name": "Mark",
    "age": 28,
    "city": "Cairo"
  },
  "3": {
    "name": "Bombom",
    "age": 27,
    "city": "Cairo"
  }
}
```

## Bonus: Changing JSON Orientation

As a bonus, we will see now how to change the JSON orientation using this API.

Request:
```json
{
  "data": [
      {"name":"John", "age":31, "city":"New York"},
      {"name":"Omar", "age":28, "city":"Cairo"},
      {"name":"Mark", "age":28, "city":"Cairo"},
      {"name":"Ahmed", "age":27, "city":"Cairo"}
  ],
  "source_orient": "records",
  "required_orient": "index"
}
```

Response:
```json
{
    "0": {
        "name": "John",
        "age": 31,
        "city": "New York"
    },
    "1": {
        "name": "Omar",
        "age": 28,
        "city": "Cairo"
    },
    "2": {
        "name": "Mark",
        "age": 28,
        "city": "Cairo"
    },
    "3": {
        "name": "Ahmed",
        "age": 27,
        "city": "Cairo"
    }
}
```