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

how to update each object in a json array using jq

$
0
0

I have an array of objects that looks like this:

[    {"host": "1.exampl.com", "path": "/dir1/file.html"},    {"host": "2.exampl.com", "path": "/dir2/file.html"},    {"host": "3.exampl.com", "path": "/dir3/file.html"}]

and I want to add a new key in each of those objects, which will create the URL using the host and path, so I would look like this:

[    {"host": "1.exampl.com", "path": "/dir1/file.html", "url": "http://1.example.com/dir1/file.html"},    {"host": "2.exampl.com", "path": "/dir2/file.html", "url": "http://2.example.com/dir2/file.html"},    {"host": "3.exampl.com", "path": "/dir3/file.html", "url": "http://3.example.com/dir3/file.html"}]

how can I do that using jq.

I tried doing ".[] |= . { . + {\"url\": \"http://\" + .host + .path } }"but this didn't work and it returned this error:jq: error: syntax error, unexpected '}', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:


Viewing all articles
Browse latest Browse all 527

Trending Articles



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