Post

JSON Parse & Filter

A simple JSON parser and that acts like jq and can be used in the browser.

ALL processing is done client side using JavaScript. No data is sent to any server nor stored in local or session storage.

Filter Expression

JSON Input

Filtered Output


Example Filter Expressions

Sample JSON Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
  "people": [
    {
      "name": "John",
      "city": "Melbourne"
    },
    {
      "name": "Greg",
      "city": "Hobart"
    },
    {
      "name": "Ben",
      "city": "Adelaide"
    }
  ],
  "metadata": [
    {
      "key": "version",
      "value": "1.1"
    },
    {
      "key": "tags",
      "value": ["people", "cities", "json", "filtering"]
    }
  ]
}

Example 1 - Object Key

Filter

1
people

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
  {
    "name": "John",
    "city": "Melbourne"
  },
  {
    "name": "Greg",
    "city": "Hobart"
  },
  {
    "name": "Ben",
    "city": "Adelaide"
  }
]

Example 2 - Array Index

Filter

1
people[0]

Output

1
2
3
4
{
  "name": "John",
  "city": "Melbourne"
}

Example 3 - Negative Array Index

  • -1 means the last Array element.
  • -2 means the second to last Array element.
  • etc.

Filter

1
people[-1]

Output

1
2
3
4
{
  "name": "Ben",
  "city": "Adelaide"
}

Example 4 - Nested Object Key

Filter

1
people[0].city

Output

1
Melbourne

Example 5 - Object Key from ALL Array Elements

Filter

1
people[].name

Output

1
2
3
4
5
[
  "John",
  "Greg",
  "Ben"
]

Example 6 - Array Function (Sort)

Calls the sort() method on the Array to sort the elements of the Array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.

Filter

1
people[].name.sort()

Output

1
2
3
4
5
[
  "Ben",
  "Greg",
  "John"
]

Example 7 - Array Function (Reverse)

Calls the reverse() method on the Array. The first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.

Filter

1
people[].name.sort().reverse()

Output

1
2
3
4
5
[
  "John",
  "Greg",
  "Ben"
]

Example 8 - Array Function (Flat)

Calls the flat() method on the Array to create a new array with all sub-array elements concatenated into it.

  • A single call to flat() will only perform this to a depth of 1. The function can be called multiple times if required.
  • MDN Web Docs - Array flat()

Filter

1
metadata[].value.flat()

Output

1
2
3
4
5
6
7
[
  "1.1",
  "people",
  "cities",
  "json",
  "filtering"
]

Example 9 - Object Keys

Calls the Object.keys() method with the current object as input. Returns an array of the given object’s own enumerable string-keyed property names.

Filter

1
people[0].keys()

Output

1
2
3
4
[
  "name",
  "city"
]

Example 10 - Object Values

Calls the Object.values() method with the current object as input. Returns an array of the given object’s own enumerable string-keyed property values.

Filter

1
people[0].values()

Output

1
2
3
4
[
  "John",
  "Melbourne"
]
This post is licensed under CC BY 4.0 by the author.