-
|
Consider this simple file:
{
"foo": 12.3456789,
"bar": 98.7654321
}I want to updated it by rounding the values to two decimal places. From all over the internet, I learn that is the way to go, and I want to use the update assignment With jq v1.8.0 I run: jq '. |= (.foo = (.foo * 100 | round / 100))' < in.jsonand get the expected result: {
"foo": 12.35,
"bar": 98.7654321
}❓️ How can I update
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hi, your close! maybe you want something like this: (.foo, .bar) |= ((. * 100) | round / 100)or using a function and a bit simpler assignment: def f: ((. * 100) | round / 100); .foo |= f | .bar |= fthe docs has a section about assignment that might be useful and also the wiki has a even deeper description how it works and is implemented https://github.com/jqlang/jq/wiki/jq-Language-Description#assignments |
Beta Was this translation helpful? Give feedback.
-
|
Close again! :) you probably want: def round_two: ((. * 100) | round / 100);
.some.nesting.[].more.nesting |=
with_entries(
# notice the "|" instead of "," so that you keep massaging the same value instead of outputting two values
# you could also use |= and skip to repeat .key
.value |= (.foo = (.foo | round_two) | .bar = (.bar | round_two))
)another way without def round_two: ((. * 100) | round / 100);
(.some.nesting[].more.nesting | .A, .B | .foo, .bar) |= round_two so how assignment like |
Beta Was this translation helpful? Give feedback.
Close again! :) you probably want:
another way without
with_entriescould beso how assignment like
LHS = RHSandLHS |= RHSetc works is thatLHSis an expression that somehow indexes the input and outputs zero or more "paths", then for each such output th…