Table of contents
1.
Introduction
2.
Path compatibility and JSON key names 
3.
Support for JSONPath (RedisJSON v2) 
4.
The syntax for legacy paths (RedisJSON v1) 
5.
Path evaluation time complexity 
6.
Frequently asked questions
6.1.
Is RedisJSON an open-source project? 
6.2.
What does a JSON database entail?  
6.3.
What is the purpose of JSONPath?  
6.4.
Is JSONPath efficient? 
6.5.
In Java, what is JSONPath? 
7.
Conclusion
Last Updated: Mar 27, 2024

RedisJSON JSONPath

Introduction

In this article, we will go through the RedisJSON JSONPath. RedisJSON creates its route syntax because there isn't one. The syntax of RedisJSON is based on industry best practices and is meant to look like JSONPath. 

RedisJSON currently supports JSONPath syntax and a heritage path syntax from RedisJSON’s initial version. 

Depending on the initial character of the path query, RedisJSON selects which syntax to employ. JSONPath syntax is used if the question begins with the symbol $. Otherwise, the traditional path syntax is used.

Path compatibility and JSON key names 

A JSON key can be any valid JSON string by definition. On the other hand, Paths are generally based on the variable name rules of JavaScript (and Java). While it is possible to store objects with arbitrary key names in RedisJSON, you can only retrieve these keys via a path provided they follow specific naming syntax rules: 

  • A letter, a dollar symbol ($), or an underscore (_) character must come first in a name. 
     
  • Letters, numerals, dollar signs, and underscores can all be used in names. 
     
  • Case matters in names.

Support for JSONPath (RedisJSON v2) 

JSONPath is now supported in RedisJSON 2.0. It adheres to the Goessner syntax given in his article. 

A JSONPath query can return results from many JSON documents. The JSON commands in this scenario apply the operation to every feasible place. This is a significant improvement over the previous query, which only works on the first path. 

When using JSONPath, the structure of the command response typically varies. For more information, see the Commands page. 

Bracket notation is supported in the new syntax, allowing special characters like colon ":" or whitespace to be used in critical names.

The syntax for legacy paths (RedisJSON v1) 

The implementation in the original version of RedisJSON was as follows. RedisJSON v2 continues to support it. 

A RedisJSON value's path always starts at the root. A period character denotes the source (.). It is optional to prefix paths that reference the root's children with the root. 

For object key access, RedisJSON supports both dot notation and bracket notation. The paths below all refer to the bar, which is a foo child under the root: 

  • .foo.bar
     
  • foo["bar"]
     
  • ['foo']["bar"]

Enclose the index of an array element in a pair of square brackets to access it. The index is based on a 0-based system, with 0 being the first element of the array, 1 being the next, and so on. Negative offsets can be used to access features from the array's beginning. The last entry in the collection, for example, is -1, the second to last element is -2, and so on.

Path evaluation time complexity 

The temporal complexity of searching for (navigating to) a path element is computed as follows: 

  • Every child-level along the path adds one additional search. 
     
  • The amount of keys in the parent object is N. Hence key search is O(N). 
     
  • O array search (1)

This indicates that searching for a path takes O(N*M) time, where N is the depth and M is the number of parent object keys. 

While this is fine for items with a small N, access to larger objects can be optimized. This enhancement is planned for a future release.

Path defaults to root if not provided. Returns null if the key or path does not exist.

Frequently asked questions

Is RedisJSON an open-source project? 

The Redis Source Available License applies to Redis Ltd.'s modules (such as RediSearch, RedisGraph, RedisJSON, RedisML, and RedisBloom) (RSAL). Read on to learn more about RSAL. Redis Enterprise is a closed-source application that requires a Redis Ltd business license. 

What does a JSON database entail?  

A JSON document database is a nonrelational database that stores and queries data as JSON documents rather than standardizing it across several tables, each with its own unique and fixed structure, as in a relational database.

What is the purpose of JSONPath?  

JSONPath, like XPath for XML, is a query language for JSON. It allows you to choose data from a JSON document and extract it. To traverse the path to an element in the JSON structure, you use a JSONPath expression.

Is JSONPath efficient? 

For huge JSON files, JsonPath appears to be somewhat sluggish. I'd like a user to be able to pass a whole query as a string in my app. JsonPath was used since it allows you to do an entire query like $. 

In Java, what is JSONPath? 

JsonPath is an easy way to extract portions of a document in JSON, similar to XPATH in XML. Many programming languages, including Javascript, Python, and PHP, support JsonPath. JsonPath lets you build a JSON path string to reuse it several times or compile and apply it all at once.

Conclusion

This article has gone through RedisJSON JSONPath. JSONPath, like XPath for XML, is a query language for JSON. It allows you to choose data from a JSON document and extract it. To traverse the path to an element in the JSON structure, you use a JSONPath expression. If you want to learn more, check out our articles in Library, where you can find everything about Redis,  Interview Experiences, and other guided paths.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Ninja, have a great time learning.

Live masterclass