jsonparsingamazon-s3jq

JQ extract parent folder


I have an s3 route and I have to split it in bucket, prefix and key using JQ.

"s3://bucket/folder1/folder2/folder3/key.txt" ->

{
  bucket: bucket,
  prefix: folder1/folder2/folder3,
  key: key.txt
}

Solution

  • Quick / manual solution:

    If you split on both // and / you can use $foo[0] to get the bucket, use $foo[-1] for the key (last index) and join the rest (1:-1) to get the prefix.

    split("//")[1] | split("/") | {
        bucket: .[0],
        prefix: .[-1],
        key: .[1:-1] | join("/")
    }
    

    Demo