Home > Back-end >  Replace string with Bash variable in jq command
Replace string with Bash variable in jq command

Time:11-16

I realize this is a simple question but I haven't been able to find the answer. Thank you to anyone who may be able to help me understand what I am doing wrong.

Goal: Search and replace a string in a specific key in a JSON file with a string in a Bash variable using jq.

For example, in the following JSON file:

"name": "Welcome - https://docs.mysite.com/",

would become

"name": "Welcome",

Input (file.json)

[
  {
    "url": "https://docs.mysite.com",
    "name": "Welcome - https://docs.mysite.com/",
    "Ocurrences": "679"
  },
  {
    "url": "https://docs.mysite.com",
    "name": "Welcome",
    "Ocurrences": "382"
  }
]

Failing script (using variable)

siteUrl="docs.mysite.com"

jq --arg siteUrl "$siteUrl" '.[].name|= (gsub(" - https://$siteUrl/"; ""))' file.json > file1.json`

Desired output (file1.json)

[
  {
    "url": "https://docs.mysite.com",
    "name": "Welcome",
    "Ocurrences": "679"
  },
  {
    "url": "https://docs.mysite.com",
    "name": "Welcome",
    "Ocurrences": "382"
  }
]

I've tried various iterations on removing quotes, changing between ' and ", and adding and removing backslashes.

Successful script (not using variable)

jq '.[].name|= (gsub(" - https://docs.mysite.com/"; ""))' file.json > file1.json

More specifically, if it matters, I am processing an export of a website's usage data from Azure App Insights. Unfortunately, the same page may be assigned different names. I sum the Ocurrences of the two objects with the newly identical url later. If it is better to fix this in App Insights I am grateful for that insight, although I know Bash better than Kusto queries. I am grateful for any help or direction.

CodePudding user response:

Almost. Variables are not automatically expanded within a string. You must interpolate them explicitly with \(…):

jq --arg siteUrl 'docs.mysite.com' '.[].name |= (gsub(" - https://\($siteUrl)/"; ""))' file.json

Alternatively, detect a suffix match and extract the prefix by slicing:

jq --arg siteUrl 'docs.mysite.com' '" - https://\($siteUrl)/" as $suffix | (.[].name | select(endswith($suffix))) |= .[:$suffix|-length]' file.json
  • Related