How can I completely sort arbitrary JSON using jq?

后端 未结 2 1536
忘了有多久
忘了有多久 2021-02-08 02:35

I want to diff two JSON text files. Unfortunately they\'re constructed in arbitrary order, so I get diffs when they\'re semantically identical. I\'d like to use jq (or whateve

2条回答
  •  后悔当初
    2021-02-08 02:48

    Here is a solution using a generic function sorted_walk/1 (so named for the reason described in the postscript below).

    normalize.jq:

    # Apply f to composite entities recursively using keys[], and to atoms
    def sorted_walk(f):
      . as $in
      | if type == "object" then
          reduce keys[] as $key
            ( {}; . + { ($key):  ($in[$key] | sorted_walk(f)) } ) | f
      elif type == "array" then map( sorted_walk(f) ) | f
      else f
      end;
    
    def normalize: sorted_walk(if type == "array" then sort else . end);
    
    normalize
    

    Example using bash:

    diff <(jq -S -f normalize.jq FILE1) <(jq -S -f normalize.jq FILE2)
    

    POSTSCRIPT: The builtin definition of walk/1 was revised after this response was first posted: it now uses keys_unsorted rather than keys.

提交回复
热议问题