Search code examples
jsonbashbase64jqgunzip

gzip decompress a property in a json file using bash


I have to verify that the base64 encoded and gzipped contents of a property in a json file are the same as the raw ones using bash.

I have been able to extract the property using jq:

cat src/my-file.json | jq '.Attachment[] | .Contents["@Value"] | @base64d'

I have tried using gzip to decompress this using @gzipd filter

jq: error (at :798): gzipd is not a valid format

and piping the value to gunzip command:

gunzip: unknown compression format

I have tried writing the contents to a file named test.gz and then using gunzip.

cat src/my-file.json | jq '.Attachment[] | .Contents["@Value"] | @base64d' > test.gz

gunzip: test.gz: not in gzip format


Solution

  • From #1931:

    Let $B be an arbitrary base64 string, then $B | @base64d is undefined if base64 -D <<< $B is not a valid UTF-8 string.

    Below is a quick workaround; output raw base64 string and decode it using base64 utility:

    jq -r '.Attachment[] | .Contents["@Value"]' src/my-file.json | base64 -d | gunzip