Rizwan Saeed
Rizwan Saeed

Reputation: 153

jq split string and assign

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?

Upvotes: 14

Views: 32725

Answers (1)

peak
peak

Reputation: 116750

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)

Upvotes: 22

Related Questions