All contributions

itchyny/gojq

gojq

A pure Go implementation of jq, for processing JSON on the command line and as a library inside Go programs.

Language
Go
GitHub stars
3,821
Merged changes
1

What we changed

The |= update operator no longer changes one value in several places at once: when an updated value is handed to a query, later updates work on a copy of it, and the output matches jq.

What this work shows

  • A query compiler and virtual machine in Go
  • Aliasing (shared values) and in-place mutation
  • jq compatibility
What was broken

One update changed several places

When an update path was a prefix of an earlier one, getpath handed the update function a value from the allocator, which it could store in several places. The query (.a.b, .a, .a.p.z) |= {p: ., q: .} added "z" to both .a.p and .a.q, while jq adds it only to .a.p.

How we fixed it

A value that escapes gets copied

A new funcGetpathWithAllocator releases the allocator when it hands out a value the allocator created, so later updates copy it. Two tests in cli/test.yaml cover overlapping paths, nested ones included.

#306

Merged change 1

  1. #306 fix: release the allocator when an updated value escapes to a query Merged 14 September 2026

What it taught us

An optimisation that modifies a value in place is safe only while nobody else holds a reference to it. The reference jq implementation set the expected output, and the tests cover overlapping paths, nested ones included.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.