[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

Topics

Choose your language

InfoQ Homepage News SQLite 3.38 Improves JSON Queries, Error Messages, and More

SQLite 3.38 Improves JSON Queries, Error Messages, and More

This item in japanese

Mar 16, 2022 2 min read

Write for InfoQ

Feed your curiosity. Help 550k+ global
senior developers
each month stay ahead.
Get in touch

SQLite 3.38.0 is the latest release of SQLite, bringing improved syntax for JSON queries, a new diagnostic interface, CLI enhancements, and more.

SQLite 3.38.0 introduces new -> and ->> operators to allow developers to define queries over JSON data. Both operators select a subcomponent of the JSON operand to their left based on the JSON path expression to their right, e.g. json_field->'$.json_component'. The difference between the two operators is -> returns a JSON value type, while ->> returns the SQL datatype extracted from a JSON value, such as text, integer, and so on.

For example,

'{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]'

yields '{"f":7}'. Similarly,

'{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2].f'

yields '7', while

'{"a":2,"c":[4,5,{"f":7}]}' ->> '$.c[2].f'

yields the integer value 7.

This is how you can select all rows in a table with a simple JSON record stored in json_col:

select * from test_table where json_col->>'$.json_key' = 'some_value';

If you are used to json_extract, it is worth noting that it differs slightly from -> and ->> in that it combines their effects together. That means, json_extract extracts a component from a JSON value and returns it as a JSON value in case of arrays and objects, and as an SQL value for strings, numeric values, etc.

The syntax SQLite supports for JSON paths is compatible with both MySQL's and PostgreSQL's. This means SQLite will understand X (PostgreSQL style) as the same as $.X (MySQL style) if X is a text label, and n as the same as $[n].

On a related note, JSON support is on by default in version 3.38 and does not require rebuilding the library using the -DSQLITE_ENABLE_JSON1 option.

SQLite 3.38.0 also tries to ease a pain point for developers caused by its usually terse error messaging. To make it easier to identify exactly where a syntax error occurs in an SQL query, the sqlite3_error_offset() can sometimes narrow it down to a specific character.

As a final note, SQLite CLI provides now enhanced support for columnar output modes by correctly handling tabs and newlines as well as supporting options like "--wrap N", "--wordwrap on", and "--quote". Additionally, the .import command is now able to automatically disambiguate column names.

Do not miss the official release note for a complete list of all new features in SQLite 3.38.0.

About the Author

Sergio De Simone

Show moreShow less

Rate this Article

Adoption
Style

This content is in the JSON topic

Related Topics:

Related Content

The InfoQ Newsletter

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /