1

I have to replace a value in a json file with the value of a bash variable. This is the sample json.

{   "swagger": "2.0",   "info": {
  "title": "DataSyncGtest",
  "version": "2.0",
  "description": "Lorem,ipsum,dorem"   },   "host": "stg.api.realogyfg.com",   "basePath": "/v2/xxx/yyy/Newproxytest" 
}

I used the below command and was able to replace the value of the basePath(Thanks to @Cyrus for pointing me with the right answers that helped). Command:

foo=/v1/proxytest
cat $NAME | jq --arg foo "$foo" '.basePath = $foo'  > test.json

But am doing this from a bash task in Azure DevOps and it adds the below instead of just the value of the variable. I'm not sure how this can be avoided.

{
  "swagger": "2.0",
  "info": {
    "title": "DataSyncGtest",
    "version": "2.0",
    "description": "blah"
  },
  "host": "stg.api.realogyfg.com",
  "basePath": "C:/Program Files/Git/v1/proxytest",
}
AK123
  • 365
  • 6
  • 17
  • if you run `bash -x yourscript` (to run the script with each command it runs logged), please edit the question to include both the `foo=` line *and* the `jq` line as logged. Frankly, it's hard to believe that `jq` itself is generating the stated output without something else going on; using `bash -x` will show commands *as they're actually run*, which can do a lot of good to help isolate content that needs to be included in an example to make it a valid [mcve]. – Charles Duffy Aug 13 '19 at 16:55
  • ...which is to say, if `bash -x yourscript` shows `foo='C:/Program Files/Git/v1/proxytest'` or `jq --arg foo 'C:/Program Files/Git/v1/proxytest'`, those are things we would need to know to be able to accurately diagnose the issue at hand. – Charles Duffy Aug 13 '19 at 16:57
  • You're showing both the lines *as you typed them in your script*, not both the lines *as they're actually run by the interpreter*. Any variance between those is relevant. – Charles Duffy Aug 13 '19 at 16:58
  • Thanks a ton! Issue was with the agent that had changed to a default VS 2017 agent, instead of Ubuntu agent. Once I corrected that, it corrected it. Thanks for the comment though, this pointed me at checking everything in the pipeline. – AK123 Aug 13 '19 at 17:08
  • Glad to hear you've got everything sorted! By the way, not related to your immediate problem, but it's a better habit to be in to use `<"$NAME"` instead of `cat "$NAME" |` -- the former gives the program a real seekable file handle, whereas the latter gives it a FIFO that can only be read from once, front-to-back; many programs, like `tail` or `sort` or `wc -c`, can operate far more efficiently with the real handle, being able to either split into threads read and process different parts of the file in parallel (as with `sort`), jump straight to the end without reading the rest (`tail`), etc. – Charles Duffy Aug 13 '19 at 17:10

0 Answers0