'Split string into fields in 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?



Solution 1:[1]

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

{item} + (.url / "/" | {number: .[-2], room: .[-1]})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1