Programming

JSONata: Filter an Array to Find a Specific IP Address

Use JSONata to filter an unordered array and return a specific IP address. Covers predicate queries, $filter, $contains, $indexOf and regex $match examples.

1 answer 1 view

How to search for and return a specific IP address from an unordered array in JSONata?

I have a JSON object with an array of IP addresses like this:

json
"host.ip{}": [
 "192.168.1.102",
 "172.16.5.30",
 "192.168.40.4"
]

The order of IPs in the array is not fixed. How can I use JSONata to find a specific IP (e.g., “172.16.5.30”) and return only that value?

In JSONata, you can filter an unordered array like host.ip{} to find and return a specific IP address such as “172.16.5.30” using a simple predicate query: `host.ip{}`[?(@ == "172.16.5.30")][0]. The backticks escape the tricky key name with its dots and braces, while the [0] grabs just the first (and only) match as a string, no array wrapper. This works no matter where the IP sits in the list—pure magic for dynamic JSON data.


Contents


The Challenge of Unordered Arrays in JSONata

Picture this: your JSON holds an array of IP addresses under a funky key like "host.ip{}", and the order flips every time—maybe from logs, sensors, or API responses. You need to snag one specific IP, say “172.16.5.30”, without caring about position. JSONata shines here as a lightweight query language for JSON, inspired by XPath, letting you transform and filter data on the fly.

Why does order not matter? JSONata treats arrays as collections, not sequences. Tools like predicates scan everything efficiently. But first, escape that key—dots and braces demand backticks: `host.ip{}`. Miss this, and you’ll hit parse errors. Straight from the official JSONata docs on simple queries, it’s the non-negotiable first step.


Mastering Predicate Queries for IP Matching

Predicates are your go-to for array filtering in JSONata. Slap [?(condition)] on the array path, and it returns matching elements. For your IP hunt:

`host.ip{}`[?(@ == "172.16.5.30")]

This spits back an array with just the match. Want the raw string? Append [0]:

`host.ip{}`[?(@ == "172.16.5.30")][0]

@ means “this item.” Boom— “172.16.5.30” every time. Operators flex too: <, >, in for arrays. The JSONata expressions guide covers how and/or build complex checks, like [@ > "172.0.0.0" and @ < "173.0.0.0"].

Test it mentally. Array: ["192.168.1.102", "172.16.5.30", "192.168.40.4"]. Result? Spot on, position irrelevant.


Leveraging the $filter Function

Need more control? $filter takes an array and a function, returning all hits. Explicit, reusable—great for bigger expressions.

$filter(`host.ip{}`, function($v) { $v == "172.16.5.30" })[0]

$v is the value; compare away. As noted in the simple queries documentation, it’s predicate-like but function-driven. Perfect if you’re chaining logic or passing dynamic values.

And yeah, [0] strips the array again. What if multiples match? It grabs 'em all—handle with $length or slices.


Quick Existence Checks with $contains

Sometimes you just need “yes or no.” $contains(array, value) returns true/false. Super lightweight:

$contains(`host.ip{}`, "172.16.5.30")

True if found, false otherwise. No extraction needed. The docs highlight this for quick scans, avoiding full filters when boolean suffices.

Pair it: $contains(...) ? "Found it!" : "Nope.". Efficient for conditionals in pipelines.


Locating Position with $indexOf

Curious where it hides? $indexOf(array, value) gives the zero-based index—or -1 if missing.

$indexOf(`host.ip{}`, "172.16.5.30")

Say your array is ["a", "172.16.5.30", "c"]—returns 1. Use it to slice: `host.ip{}`[$indexOf(`host.ip{}`, "172.16.5.30")]. Same as predicate, but reveals position. Docs bundle it with filtering tools for full control.


Advanced Pattern Matching via $match

IPs vary—maybe partial matches or subnets? Regex to the rescue with $match in predicates:

`host.ip{}`[?($match(@, /172.16.\d+.\d+/))][0]

Escapes dots (.), matches 172.16.x.x. $match returns matches array; truthy if any. From the official simple queries page, this handles wildcards effortlessly. Flex for “192.168.*” patterns too.

Pro tip: Test regexes separately. JSONata’s engine is picky but powerful.


Sources

  1. Simple Queries · JSONata
  2. Manipulating data with functions and expressions · JSONata
  3. 10 Examples to Help You Learn JSONata | Blues Wireless

Conclusion

JSONata makes hunting specific IPs in unordered arrays dead simple—predicates for quick grabs, $filter for power users, and extras like $contains or $match for edge cases. Start with `host.ip{}`[?(@ == "172.16.5.30")][0]; it’ll handle 99% of scenarios. Experiment in online playgrounds, and you’ll transform JSON like a pro.

Authors
Verified by moderation
Moderation
JSONata: Filter an Array to Find a Specific IP Address