Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq split string and assign

Tags:

json

jq

I have the following json

{
    "version" : "0.1.2",
    "basePath" : "/"
}

and the desired output is

{
    "version" : "0.1.2",
    "basePath" : "beta1"
}

I have the following jq which is producing the error below:

.basePath = .version | split(".") as $version | if  $version[0] == "0" then "beta"+ $version[1] else $version[0] end

jq: error (at :3): split input and separator must be strings exit status 5

Using .basePath = .version assigns the value successfully and .version | split(".") as $version | if $version[0] == "0" then "beta"+ $version[1] else $version[0] end on its own returns "beta1". Is there a way to assign the string to the basePath key?

like image 777
Rizwan Saeed Avatar asked Dec 03 '25 16:12

Rizwan Saeed


1 Answers

Good news! Your proposed solution is just missing a pair of parentheses. Also, there is no need for $version. That is, this will do it:

.basePath = (.version | split(".")
             | if .[0] == "0" then "beta"+ .[1] else .[0] end)
like image 149
peak Avatar answered Dec 06 '25 10:12

peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!