Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Noyabronok/27e3234ff7190084e19ad4ecc041f898 to your computer and use it in GitHub Desktop.
Save Noyabronok/27e3234ff7190084e19ad4ecc041f898 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

@Noyabronok
Copy link
Author

Improved to

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

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