yq icon indicating copy to clipboard operation
yq copied to clipboard

Stable canonical form output

Open Zash opened this issue 2 years ago • 1 comments

Please describe your feature request. I wish I could use yq to produce output in a stable canonical form, i.e, with consistent indentation and sorted key order. This would allow comparing semantically identical documents even if their indentation or order of key-value pairs differ, especially if consistently applied when checking into version control. When combined with --prettyPrint, the primary difference would be sorting of keys.

For JSON there exists RFC 8785 describing a canonical form, XML has a W3 recommendation. I am not currently aware of something for the other formats, notably YAML, but stable key order and general consistency would go a long way.

Describe the solution you'd like If we have data1.json like: (please keep to around 10 lines )

{
  "code": "AU",
  "country": "Australia",
  "tld": "au.",
  "capital": "Canberra"
}

And we run a command:

yq --canonical --prettyPrint data1.json

it would produce

{"capital":"Canberra","code":"AU","country":"Australia","tld":"au."}

When combined with pretty-printing it could behave something like:

yq --canonical --prettyPrint data1.json

would produce

{
  "capital" : "Canberra",
  "code" : "AU",
  "country" : "Australia",
  "tld" : "au."
}

or with YAML:

yq --canonical --prettyPrint -oy data1.json

would produce

capital: Canberra
code: AU
country: Australia
tld: au.

Describe alternatives you've considered For JSON there exists tools like json_pp -json_opt canonical,pretty but I have yet to find a normalization tool that can handle YAML.

Zash avatar Jun 26 '23 09:06 Zash

Hm, is sort_keys() and --prettyPrint enough to do this?

Zash avatar Apr 01 '24 16:04 Zash

yq -i --prettyPrint 'sort_keys(..)' somefile.yaml seems to be Good Enough for me, so I will close this.

Zash avatar May 07 '24 08:05 Zash