Primitive and compound JSON data types (2024)

This page documents the preview version (v2.23). Preview includes features under active development and is for development and testing only. For production, use the stable version (v2024.1). To learn more, see Versioning.

JSON can represent (sub)values of four primitive data types and of two compound data types.

The primitive data types are string, number, boolean, and null. There is no way to declare the data type of a JSON value; rather, it emerges from the syntax of the representation.

Compare this with SQL and PL/pgSQL. SQL establishes the data type of a value from the metadata for the column in the table, or the field in the record, into which it is written or from which it is read. It also has the typecast notation, like ::text or ::boolean to establish the data type of a SQL literal. PL/pgSQL also supports the typecast notation and establishes the data type of a variable or a formal parameter by declaration. In this way, JSON is better compared with Python, which also implements an emergent data type paradigm. It is for this reason that, in the JSON type system, null is defined as a data type rather than as a "value" (strictly, the absence of information about the value) of one of the other data types.

Notice that JSON cannot represent a date-time value except as a conventionally formatted string value.

The two compound data types are object and array.

A JSON literal is represented in a SQL statement or a PL/pgSQL program by the enquoted ::json or ::jsonb typecast of a text value that conforms to RFC 7159.

JSON string

A JSON string value is a sequence of zero, one, or many Unicode characters enclosed by the " character. Here are some examples, shown as SQL literals:

'"Dog"'::jsonb

The empty string is legal, and is distinct from the JSON null.

'""'::jsonb

Case and whitespace are significant. Special characters within a string value need to be escaped, thus:

  • Backspace:\b
  • Form feed:\f
  • Newline:\n
  • Carriage return:\r
  • Tab:\t
  • Double quote:\"
  • Backslash:\\

For example:

 '"\"First line\"\n\"second line\""'::jsonb

The explanation of the difference between the -> and ->> operators is illustrated nicely by this JSON string value.

JSON number

Here are some examples, shown as SQL literals:

'17'::jsonb

and:

'4.2'::jsonb

and:

'2.99792E8'::jsonb

Notice that JSON makes no distinction between integers and real numbers.

JSON boolean

Here are the two allowed values, shown as SQL literals:

'true'::jsonb

and:

'false'::jsonb

JSON null

As explained above, null is special in JSON in that it is its own data type that allows exactly one "value", thus:

'null'::jsonb

JSON object

An object is a set of key-value pairs separated by commas and surrounded by curly braces. The order is insignificant. The values in an object do not have to have the same data types as each other. For example:

'{ "a 1" : "Abc", "a 2" : 42, "a 3" : true, "a 4" : null, "a 5" : {"x" : 1, "y": "Pqr"}}'::jsonb

Keys are case-sensitive and whitespace within such keys is significant. They can even contain characters that must be escaped. However, if a key does include spaces and special characters, the syntax that you need to read its value can become rather complex. It is sensible, therefore, to avoid exploiting this freedom.

An object can include more than one key-value pair with the same key. This is not recommended, but the outcome is well-defined: the last-mentioned key-value pair, in left-to-right order, in the set of key-value pairs with the same key "wins". You can test this by reading the value for a specified key with an operator like ->.

JSON array

An array is an ordered list of unnamed JSON values—in other words, the order is defined and is significant. The values in an array do not have to have the same data types as each other. For example:

'[1, 2, "Abc", true, false, null, {"x": 17, "y": 42}]'::jsonb

The values in an array are indexed from 0. See the account of the -> operator.

Example compound JSON value

{ "given_name" : "Fred", "family_name" : "Smith", "email_address" : "[email protected]", "hire_date" : "17-Jan-2015", "job" : "sales", "base_annual_salary" : 50000, "commisission_rate" : 0.05, "phones" : ["+11234567890", "+13216540987"]}

This is a JSON object with eight fields. The first seven are primitive string or number values (one of which conventionally represents a date) and the eighth is an array of two primitive string values. The text representations of the phone numbers follow a convention by starting with + and (presumably) a country code. JSON has no mechanisms for defining such conventions and for enforcing conformance.

Note

To see how these limitations can be ameliorated when a JSON document is stored in a column in a SQL table, see Create indexes and check constraints on json and jsonb columns.

In general, the top-level JSON document is an arbitrarily deep and wide hierarchy of subdocuments whose leaves are primitive values.

Notably, and in contrast to XML, JSON is not self-describing. Moreover, JSON does not support comments. But the intention is that the syntax should be reasonably intuitively obvious and human-readable.

It is the responsibility of the consumers of JSON documents to discover the composition rules of any corpus that they have to deal with by ad hoc methods—at best external documentation and at worst human (or mechanical) inspection.

Most programming languages have data types that correspond directly to JSON's primitive data types and to its compound object and array data types.

Note

Because YSQL manipulates a JSON document as the value of a json or jsonb table-row intersection or PL/pgSQL variable, the terms "json [sub]value" or "jsonb [sub]value" (and JSON value as the superclass) are preferred from now on in this section—using "value" rather than "document".

Primitive and compound JSON data types (2024)
Top Articles
Bankruptcy Timeline
17 ways to increase your bookings on Airbnb
Srtc Tifton Ga
Safety Jackpot Login
Joliet Patch Arrests Today
Wordscapes Level 6030
What Happened To Dr Ray On Dr Pol
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Fusion
Directions To 401 East Chestnut Street Louisville Kentucky
Farmers Branch Isd Calendar
Bhad Bhabie Shares Footage Of Her Child's Father Beating Her Up, Wants Him To 'Get Help'
Decaying Brackenhide Blanket
Day Octopus | Hawaii Marine Life
fltimes.com | Finger Lakes Times
Transformers Movie Wiki
Gas Station Drive Thru Car Wash Near Me
Nitti Sanitation Holiday Schedule
Magicseaweed Capitola
Becu Turbotax Discount Code
Puretalkusa.com/Amac
Byui Calendar Fall 2023
Craigslist Mt Pleasant Sc
Icivics The Electoral Process Answer Key
Optum Urgent Care - Nutley Photos
Morse Road Bmv Hours
What Time Does Walmart Auto Center Open
European city that's best to visit from the UK by train has amazing beer
Bellin Patient Portal
University Of Michigan Paging System
Booknet.com Contract Marriage 2
Lininii
Abga Gestation Calculator
2015 Chevrolet Silverado 1500 for sale - Houston, TX - craigslist
Tamil Play.com
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
Movies123.Pick
Back to the Future Part III | Rotten Tomatoes
Zero Sievert Coop
Ljw Obits
Is Arnold Swansinger Married
Aliciabibs
8 Ball Pool Unblocked Cool Math Games
Directions To Advance Auto
Puretalkusa.com/Amac
Aita For Announcing My Pregnancy At My Sil Wedding
Wunderground Orlando
Dr Mayy Deadrick Paradise Valley
Martha's Vineyard – Travel guide at Wikivoyage
Why Are The French So Google Feud Answers
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Jovan Pulitzer Telegram
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5926

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.