Search code examples
unixtrimjq

How to trim white space for every element in JQ?


I have the following simple JSON

json='[{"k1":" http://url", "k2":null, "k3":" v3", "k4":" v4"}]'

what I need is:

"http://url",null

(without a space before v1 and v2)

What I have is:

echo $json | jq -c '.[] | .k1, .k2 ' | paste -d "," - -

How to get rid of the space in k1 and k2 values?


Solution

  • The below will remove leading and trailing spaces in strings anywhere inside an arbitrarily complex JSON structure:

    #!/usr/bin/env jq -cf
    
    # Define walk function (standard library only for jq 1.6 and newer, currently unreleased)
    def walk(f):
      . as $in
      | if type == "object" then
          reduce keys_unsorted[] as $key
            ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
      elif type == "array" then map( walk(f) ) | f
      else f
      end;
    
    walk(
      if type == "string" then
        (sub("^[[:space:]]+"; "") | sub("[[:space:]]+$"; ""))
      else . end
    )
    

    If one saves the above (e.g. in trim-json), and marks it executable (chmod +x trim-json), then ./trim-json <<<"$json" with your given input emits:

    [{"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}]
    

    Similarly, with the updated input:

    $ json='[{"k1":" http://url", "k2":null, "k3":" v3", "k4":" v4"}]'
    $ ./trim-json <<<"$json"
    [{"k1":"http://url","k2":null,"k3":"v3","k4":"v4"}]