Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment a number when transforming json

Tags:

json

bash

shell

jq

I am trying to increment a version field. Input is

{"version":1}

Output should be

{"v":2}

When I do

echo '{"version":1}'|jq '{"v":.version+1}'

I get

error: syntax error, unexpected '+', expecting '}'

While adding in string interpolation works

echo '{"version":1}'|jq '{"v":"\(.version+1)"}'

yields

{
  "v":"2"
}

I need v to be of number type, though.

like image 295
neurolabs Avatar asked Sep 27 '22 11:09

neurolabs


1 Answers

Try

echo '{"version":1}' | jq '{"v":(.version+1)}'

This seems to work on the playground of jq.

like image 80
Rambo Ramon Avatar answered Oct 03 '22 21:10

Rambo Ramon