2

guys.

I need to replace all occurrences of a string with a different string in the whole body of a JSON in JQ.

Say in the following input JSON I want to replace the string "DEV" with "INT". How could I do this in JQ?

Input JSON:

{  
   "startIndex":0,
   "vXPolicies":[  
      {  
         "policyName":"DEVXPolicyDEF",
         "tables":"DEVX_DE_DEF"             
      },
      {  
         "policyName":"DEVXPolicyABC",
         "tables":"DEVX_DE_ABC"
      }
   ]
}

Desired output JSON:

{  
   "startIndex":0,
   "vXPolicies":[  
      {  
         "policyName":"INTXPolicyDEF",
         "tables":"INTX_DE_DEF"
      },
      {  
         "policyName":"INTXPolicyABC",
         "tables":"INTX_DE_ABC"
      }
   ]
}

Thank you!

MadaIdo
  • 33
  • 1
  • 3

1 Answers1

3

This appears to be a case for walk/1 but I'm not sure whether you want the semantics of sub or gsub. For the sake of illustration, let's use gsub:

walk(if type == "string" then gsub("DEV"; "INT") else . end)

Of course if you want the key names checked as well, the above would have to be adjusted accordingly.

If your jq does not have walk/1, then consider upgrading to master or include its definition, which is available from https://github.com/stedolan/jq/blob/master/src/builtin.jq

peak
  • 105,803
  • 17
  • 152
  • 177