Skip to content

Differences between JSON output formats

Lukas Fittl edited this page Dec 26, 2015 · 2 revisions

libpg_query returns the PostgreSQL AST as JSON, based on custom helper methods that take the internal binary representation.

There are currently two formats of JSON output supported:

Version 1 (Default)

Used by pg_query since inception, and used my almost (?) all other libraries and tools.

  • Based on Postgres' built-in node output format (it shares the same oddities)
  • Applied as a patch inside the tree (needs a rebase with every major release)
  • Outputs all node names as uppercase (e.g. "A_CONST")
  • Outputs value nodes as the actual values ** This leads to problems with overly long integers or floats (returned as strings inside Float nodes in V2)
  • Outputs node lists as JSON arrays ([{"Node": ...}, {"Node": ...}])
  • Outputs integer/OID lists as JSON arrays ([1, 2, ...])

Version 2 (Currently being tested)

This newer version is designed to match the Postgres source itself more closely, and enable automatic generation for newer PostgreSQL versions that are released. Any future format changes are focused on V2 or later.

Used by pg_query.go and is developed with its native structs in mind.

  • Generated based on the given Postgres headers, source files outside tree (much easier to upgrade)
  • Outputs all node names as they are called in Postgres source (e.g. "A_Const")
  • Outputs value nodes as nodes of that type (e.g. {"Float": {"str": "51.1992411"}})
  • Outputs node lists as JSON arrays ([{"Node": ...}, {"Node": ...}])
  • Outputs integer/OID lists as IntList/OidList nodes ([{"IntList": {"items": [1, 2, ...]}}])

In order to get V2 output you need to call make like make JSON_OUTPUT_V2=1.