Skip to content

fix(jsonview): preserve literal object keys in pretty output - #86

Open
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/pretty-literal-object-keys
Open

fix(jsonview): preserve literal object keys in pretty output#86
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/pretty-literal-object-keys

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Use literal object-member lookup when rendering pretty JSON so keys containing GJSON path syntax display their own values.

Fixes #81.

Problem

The pretty renderer enumerates real JSON member names with @keys, then looks each value up again through result.Get(key.Str).

Result.Get does not perform literal map lookup. It parses the supplied string as a GJSON path. That changes the meaning of keys such as "a.b".

For example:

{
  "a.b": "literal-value",
  "a": {
    "b": "nested-value"
  }
}

The current renderer enumerates "a.b" correctly but then resolves result.Get("a.b") to the nested a.b value. The row for the literal top-level key can therefore display "nested-value", and "literal-value" may never appear in the output.

Root cause

Two incompatible access models are mixed in the same loop:

  • @keys returns literal object member names;
  • Result.Get treats those names as path expressions.

This is only safe for keys that contain no GJSON path metacharacters.

Fix

Materialize the object's literal map once:

values := result.Map()

and then index it with the enumerated key:

value := values[key.Str]

This also avoids repeatedly reparsing each key as a path.

Regression coverage

Added a focused regression with both:

  • a top-level key named "a.b" whose value is "literal-value";
  • a nested a -> b value named "nested-value".

The test requires both values to remain visible in static pretty output. On current main, the literal value is lost because the dotted key resolves the nested path.

Validation

The branch is based directly on upstream main at d082a010f7c6cacf407d8a1581446a7857f9f1bb and is not behind it.

Production diff: 2 additions and 1 deletion in internal/jsonview/staticdisplay.go, plus one focused regression file.

Full repository validation is left to the repository's GitHub Actions checks.

Risk

Low. The renderer already has the literal key names. The change only uses ordinary object-member lookup for those keys instead of reinterpreting them as query expressions. Objects with simple keys retain the same displayed values.

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.

Pretty JSON output resolves literal object keys as GJSON paths

1 participant