Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The SQL-standard's SQL/JSON description uses the term JSON PATH but does not refer to XPath. The postgres docs however say that jsonpath is inspired by XPath.

"SQL/JSON path expressions specify the items to be retrieved from the JSON data, similar to XPath expressions used for SQL access to XML"

Here is an example. The JSON table that I use here consists of millions of rows, each row with a so-called JSON document, not very large, and not very deeply nested. It seems to me that this is/will be typical usage. I retrieved test-JSON data from (h/t choroba).

One example row with its JSON content:

{ "age": 63, "bool": true, "city": "New York City", "date": "1986-04-03", "enum": "json", "array": [ "Lila", "Vanessa", "Daune", "Brandise", "Jsandye" ], "regEx": "helloooooooooooooooo" "random": 78, "country": "Iceland", "friends": [ { "country": "CA", "lastname": "Yusuk", "firstname": "Deirdre" }, { "country": "AZ", "lastname": "Suanne", "firstname": "Elfreda" }, { "country": "FM", "lastname": "Magdalen", "firstname": "Britte" } ], "lastname": "Hailee", "firstname": "Jaime", "countryCode": "IE", "random float": 6.529, "array of objects": [ { "index": 0, "index start at 5": 5 }, { "index": 1, "index start at 5": 6 }, { "index": 2, "index start at 5": 7 } ], "email from expression": "" "email uses current data": "" }

Here is a query example. The @@ is a postgresql operator, but the part in the single quotes is a jsonpath expression which is SQL/JSON-compliant.

select id, js->'friends' as friends from jsontable where js @@ '$.friends[*].country == "AZ" && $.friends[*].lastname starts with "Y" ' ; id | friends ---------+------------------------------------- 110877 | [ | { | "country": "CA", | "lastname": "Yusuk", | "firstname": "Deirdre" | }, | { | "country": "AZ", | "lastname": "Suanne", | "firstname": "Elfreda" | }, | { | "country": "FM", | "lastname": "Magdalen", | "firstname": "Britte" | } | ] 116322 | [ | { | "country": "AZ", | "lastname": "Ietta", | "firstname": "Wendi" | }, | { | "country": "NG", | "lastname": "Yuille", | "firstname": "Giustina" | }, | { | "country": "MS", | "lastname": "Tengdin", | "firstname": "Adelle" | } | ] 117778 | [ | { | "country": "CK", | "lastname": "Fancie", | "firstname": "Fanny" | }, | { | "country": "JM", | "lastname": "Jerald", | "firstname": "Mureil" | }, | { | "country": "AZ", | "lastname": "Yam", | "firstname": "Wileen" | } | ] [[...]]

This query returns ~1000 rows (above only 3 are shown) from a 3 GB table with 3 M rows. Thanks to an index on the JSON column (js), the query returns in 120 ms.

YMMV. It is probably more interesting for you to try it out on a real instance. I tried a sqlfiddle but it runs an old (near-EOL) version of postgres (9.6) that understands JSON data but cannot query in the same way as I did here locally (@@ and @? not yet implemented). I did a different search there ( json1 @> json2, i.e., json2-in-json1 containment).

sqlfiddle 1

where js @> '{"country": "Thailand"}'

sqlfiddle 2

  and js @> '{"friends": [{"country": "AE"}]}'

db-fiddle has a newer postgres (13)

In reply to Re^4: [OT] Re^3: Examples or tutorials for Perl grammars? by erix
in thread Examples or tutorials for Perl grammars? by LittleJack

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-05-20 17:30 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (75 votes). Check out past polls.