Skip to content

Add JSONFormatter for PostgreSQL JSON Array Output #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 20, 2025

This PR adds a new JSONFormatter transformer that converts SQL queries into PostgreSQL-compatible JSON-returning queries. This implementation follows the pattern from CarbunqleX, enabling developers to easily generate JSON output directly from SQL queries without manual string manipulation.

Features added

  • New JSONFormatter class that transforms SELECT queries to return JSON array results
  • Support for converting column expressions into JSON key-value pairs
  • COALESCE wrapping to handle empty result sets ([] instead of NULL)
  • Option to use either jsonb_agg or json_agg functions
  • Support for creating nested JSON structures through the groupBy option
  • Comprehensive test coverage for various query scenarios
  • Documentation in README.md with usage examples

Usage examples

Basic usage:

const query = SelectQueryParser.parse(`
  SELECT id, name, email FROM users WHERE active = true
`);

// Generate JSON-returning SQL
const jsonSql = new JSONFormatter().visit(query);

// Result:
// SELECT COALESCE(jsonb_agg(jsonb_build_object(
//   'id', "id",
//   'name', "name",
//   'email', "email"
// )), '[]') AS result
// FROM "users"
// WHERE "active" = true

Nested JSON with groupBy:

const jsonSql = new JSONFormatter({ 
  groupBy: {
    'users': ['id', 'name', 'email'],
    'orders': ['order_id', 'order_total', 'order_date']
  }
}).visit(query);

// Creates a nested structure with orders as a nested array within each user

This makes it simpler for applications to generate JSON directly from the database, which is particularly useful for API backends that need to return structured JSON data.

Fixes #14.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits May 20, 2025 12:35
Co-authored-by: mk3008 <7686540+mk3008@users.noreply.github.com>
Co-authored-by: mk3008 <7686540+mk3008@users.noreply.github.com>
Co-authored-by: mk3008 <7686540+mk3008@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Feature Request: Add JSONFormatter for PostgreSQL JSON Array Output Add JSONFormatter for PostgreSQL JSON Array Output May 20, 2025
@Copilot Copilot AI requested a review from mk3008 May 20, 2025 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add JSONFormatter for PostgreSQL JSON Array Output
2 participants