Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into fields in JQ

Tags:

json

jq

I have this JSON:

{"item":2, "url":"domain/house/23/bedroom"}

I'm trying to use jq to obtain this new JSON:

{"item":2, "number":"23", "room":"bedroom"}

Is it possible to do this in JQ? As a first step, I tried to use the capture function to catch the substring after "/house/", but it doesn't work:

cat myjson.json | jq -c '{item:.item,substring:(.url | capture("/house/.*").substring)}'

Is there a way to extract both values and put them in two different fields?

like image 290
alcor Avatar asked Dec 21 '25 04:12

alcor


1 Answers

Split url by slashes, and use the result for generating new fields.

{item} + (.url / "/" | {number: .[-2], room: .[-1]})
like image 87
oguz ismail Avatar answered Dec 23 '25 19:12

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!