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.
JSON Input
Filter Expression
Filtered Output
Functions Supported
String.parseInt()
can be called on Strings to convert a value to a Integer.String.parseFloat()
can be called on Strings to convert a value to a Float.String.json()
can be called on Strings to parse it into an Object.Object.toString()
can be called on Arrays, Objects, & Numbers to convert it to a String.Object.key()
can be called on an Object to create a new Array with the Object’s key names as elements.Object.values()
can be called on an Object to create a new Array with the Object’s values as elements.Array.sort()
can be called on an Array to Sort it.Array.reverse()
can be called on an Array to Reverse it.Array.flat()
can be called on an Array to create a new Array with all sub-array elements concatenated into it.
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 - 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 - Array Index
Filter
1
people[0]
Output
1
2
3
4
{
"name": "John",
"city": "Melbourne"
}
Example - 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 - Nested Object Key
Filter
1
people[0].city
Output
1
Melbourne
Example - Object Key from ALL Array Elements
Filter
1
people[].name
Output
1
2
3
4
5
[
"John",
"Greg",
"Ben"
]
Example - New Object with Custom Keys
Creates a new Object with the specified custom keys. The values for these key are expressions that will be evaluated.
Filter
1
{names: people[].name}
Output
1
2
3
4
5
6
7
{
"names": [
"John",
"Greg",
"Ben"
]
}
Filter
1
people[]{person: name, home: city}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"person": "John",
"home": "Melbourne"
},
{
"person": "Greg",
"home": "Hobart"
},
{
"person": "Ben",
"home": "Adelaide"
}
]
Filter
1
metadata[]{value}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
[
{
"value": "1.1"
},
{
"value": [
"people",
"cities",
"json",
"filtering"
]
}
]
Example - String.parseInt()
Calls the parseInt()
method which parses a string argument and returns an integer in base 10.
Filter
1
{result: metadata[0].value.parseInt()}
Output
1
2
3
{
"result": 1
}
Example - String.parseFloat()
Calls the parseFloat()
method which parses a string argument and returns a floating point number.
Filter
1
{result: metadata[0].value.parseFloat()}
Output
1
2
3
{
"result": 1.1
}
Example - JSON
Calls the JSON.parse()
method with the current data as input, constructing the JavaScript value or object described by the string.
Input
1
{"obj":"{\\\"name\\\":\\\"John Smith\\\"}"}
Filter
1
obj.json()
Output
1
2
3
{
"name": "John Smith"
}
Example - 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 - 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 - 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 of1
. 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 - Object toString()
Calls the JSON.stringify()
method with the current data as input. Converts a JavaScript value to a JSON string.
Filter
1
{output: people.toString()}
Output
1
2
3
{
"output": "[{\"name\":\"John\",\"city\":\"Melbourne\"},{\"name\":\"Greg\",\"city\":\"Hobart\"},{\"name\":\"Ben\",\"city\":\"Adelaide\"}]"
}
Example - 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 - 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"
]