Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

How do I use an element in a subfunction?

$
0
0

I'm trying to use the sub function to search/replace, but using JSON elements rather than literals.

Here's a simple example JSON:

{"example": {"search_for": "https://a-url-using-a-shortener.com","replace_with": "https://my.site.com","text": "I want to have the full URL, not https://a-url-using-a-shortener.com"     }}

I can search/replace the string using the following jq with literals:

.example | .text | sub( "https://a-url-using-a-shortener.com";"https://my.site.com")

And I get the result I expect:

"I want to have the full URL, not https://my.site.com"

But what I want to do is use the other elements for the search and replace:

.example | .text | sub( .search_for ; .replace_with )

But this throws up an error:

jq: error (at /dev/stdin:6): Cannot index string with string "search_for"

I've tried a few variations e.g. .search_for.value and .search_for | tostring. Have I missed something stupid?


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>