jq icon indicating copy to clipboard operation
jq copied to clipboard

Add in-place modification (expression) shorthand in objects

Open Wazzaps opened this issue 3 years ago • 2 comments

Just as {"a": .a} is shortened into {a}, I shortened {"a": .a | expr} into {a | expr}.
This is unambiguous because jq doesn't accept bare expressions inside object definitions.

I added tests and a paragraph in the manual, let me know if it's not clear enough (I'm not a native speaker).
Thank you for the awesome project :D

Comparisons

Selecting and modifying specific fields

The syntax stays consistent between selection and in-place processing

# Before:
echo '{"title": "Hello", "foo": 1, "bar": 2}' | ./jq '{foo, "title": .title | ascii_upcase}'
# After:
echo '{"title": "Hello", "foo": 1, "bar": 2}' | ./jq '{foo, title | ascii_upcase}'
# Output:
# {"foo": 1, "title": "HELLO"}

Modifying specific fields (patching)

Variant 1: Works better when modifying a value nested deeply in . Variant 2: Increases focus on the expression itself, by removing the "noisy" syntax ("a": .a)

# Before:
echo '{"title": "Hello", "foo": 1, "bar": 2}' | ./jq '.title |= ascii_upcase'
echo '{"title": "Hello", "foo": 1, "bar": 2}' | ./jq '. + {"title": .title | ascii_upcase}'
# After:
echo '{"title": "Hello", "foo": 1, "bar": 2}' | ./jq '. + {title | ascii_upcase}'
# Output:
# {"title": "HELLO", "foo": 1, "bar": 2}

Nested selection

It now resembles GraphQL queries, which are familiar to many devs, which should reduce the learning curve for them.

# Before:
echo '{"hello": {"world": {"foo": 1, "bar": 2}, "there": 3}, "misc": 4}' | ./jq '{"hello": .hello | {"world": .world | {foo}, there}}'
echo '{"hello": {"world": {"foo": 1, "bar": 2}, "there": 3}, "misc": 4}' | ./jq '{"hello": {"world": {"foo": .hello.world.foo}, "there": .hello.there}'
# After:
echo '{"hello": {"world": {"foo": 1, "bar": 2}, "there": 3}, "misc": 4}' | ./jq '{hello | {world | {foo}, there}}'
# Output:
# {"hello": {"world": {"foo": 1}, "there": 3}}

Wazzaps avatar Mar 11 '21 21:03 Wazzaps

AppVeyor CI seems broken, something with MSYS's PGP signature

Wazzaps avatar Mar 11 '21 22:03 Wazzaps

Coverage Status

Coverage increased (+0.4%) to 84.488% when pulling 193dafd71a3448724da99f68120e74c0c5f9ee56 on Wazzaps:master into 80052e5275ae8c45b20411eecdd49c945a64a412 on stedolan:master.

coveralls avatar Mar 11 '21 22:03 coveralls