Skip to content

feat: add JSON output format for diagram export - #2850

Open
pauldotknopf wants to merge 1 commit into
d2lang:masterfrom
pauldotknopf:feature/json-output-format
Open

feat: add JSON output format for diagram export#2850
pauldotknopf wants to merge 1 commit into
d2lang:masterfrom
pauldotknopf:feature/json-output-format

Conversation

@pauldotknopf

Copy link
Copy Markdown

Add .json as a supported export format that serializes the d2target.Diagram to pretty-printed JSON. This provides a structured, machine-readable representation of the rendered diagram including all shapes, connections, positions, and styling.

The JSON output skips SVG rendering entirely — it marshals the already-computed diagram after layout and export, making it the fastest output format.

Changes:

  • d2cli/export.go: Register JSON extension (.json) in supported extensions, stdout format map, and supported stdout formats
  • d2cli/main.go: Add JSON handling in _render() that marshals diagram via json.MarshalIndent and writes to output
  • d2cli/help.go: Add file.json to usage and description text
  • d2cli/export_test.go: Add test cases for file and stdout JSON output

Usage:
d2 input.d2 output.json d2 --format json input.d2 -

Add .json as a supported export format that serializes the
d2target.Diagram to pretty-printed JSON. This provides a structured,
machine-readable representation of the rendered diagram including all
shapes, connections, positions, and styling.

The JSON output skips SVG rendering entirely — it marshals the
already-computed diagram after layout and export, making it the
fastest output format.

Changes:
- d2cli/export.go: Register JSON extension (.json) in supported
  extensions, stdout format map, and supported stdout formats
- d2cli/main.go: Add JSON handling in _render() that marshals
  diagram via json.MarshalIndent and writes to output
- d2cli/help.go: Add file.json to usage and description text
- d2cli/export_test.go: Add test cases for file and stdout JSON output

Usage:
  d2 input.d2 output.json
  d2 --format json input.d2 -
@pauldotknopf

Copy link
Copy Markdown
Author

context: I need to consume that d2 layout from another language/system.

I'll be using the JSON format to load into neo4j, so that I can have an LLM reason against it.

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.

1 participant