0

Got {"a": 1, "b": 2, "c": 3}

want it be {"a": 1, "b": 6, "c": 3}

but if I {"a": 1, "b": 2, "c": 3} * {"b": 3} it ends up {"a": 1, "b": 3, "c": 3}.

How do I make it happen with jq?

  • are you sure the numbers are rly numbers or strings? https://stackoverflow.com/questions/56861281/jq-how-to-multiply-values-that-are-recognised-as-strings – JSRB Dec 26 '19 at 11:53

2 Answers2

1

If you want to multiply the .b value in an object by 3, you could write:

.b *= 3
peak
  • 105,803
  • 17
  • 152
  • 177
0

If you want to define a multiplication operation on JSON objects, consider:

def multiply(o):
  reduce (o|keys_unsorted[]) as $k (.; .[$k] *= o[$k]);

Using your example:

{"a": 1, "b": 2, "c": 3} | multiply({"b":3})

yields:

{
  "a": 1,
  "b": 6,
  "c": 3
}
peak
  • 105,803
  • 17
  • 152
  • 177