Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active October 14, 2022 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save drmalex07/c20b3f50f81d1a5e9925c501000d60a1 to your computer and use it in GitHub Desktop.
Save drmalex07/c20b3f50f81d1a5e9925c501000d60a1 to your computer and use it in GitHub Desktop.
Convert properties to JSON using jq. #properties $json #jq

README

Say we have a properties file at foo.properties.

First, clean-up whitespace and empty lines, store in foo-1.properties:

cat foo.properties | \
    sed 's/[[:space:]]*=[[:space:]]*/=/' | \
    sed 's/[[:space:]]*$//' | \
    sed '/^$/d' > foo-1.properties

Pipe to jq using raw mode (-R, to treat input as raw text lines) and slurp mode (-s to merge stream of objects into a single object)

cat foo-1.properties | \
    jq -R -s 'split("\n") | map(split("=")) | map({(.[0]): .[1]}) | add' > foo.json

See also: https://jqplay.org/s/v3fqcUGzvx

@michalkowol
Copy link

You could use only jq for that

cat foo.properties | jq -R -s 'split("\n") | map(select(length > 0)) | map(select(startswith("#") | not)) | map(split("=")) | map({(.[0]): .[1]}) | add'

@grzegorznowak
Copy link

cat foo.properties | jq -R -s 'split("\n") | map(select(length > 0)) | map(select(startswith("#") | not)) | map(split("=")) | map({(.[0]): .[1]}) | add'

thanks, this almost worked for me with exception to strings containing = breaking it.
The slightly tweaked version I'm using is:

cat foo.properties | jq -R -s 'split("\n") | map(select(length > 0)) | map(select(startswith("#") | not)) | map(split("=")) | map({(.[0]): .[1:] | join("=")}) | add'

@OneCricketeer
Copy link

You don't need cat

Usage:  jq [options] <jq filter> [file...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment