简体   繁体   中英

How can I replace a null value in a json document by a specific value with jq?

I have a json file that contains null s as values for some keys, which I would like to replace with some specific value.

Given this input:

{
  "id": null,
  "rows": [
    {
      "panels": [
        {
          "title": "Panel1",
          "datasource": null
        },
        {
          "title": "Panel2",
          "datasource": null
        }
      ]
    }
  ]
}

I would like to have

{
  "id": null,
  "rows": [
    {
      "panels": [
        {
          "title": "Panel1",
          "datasource": "mydb"
        },
        {
          "title": "Panel2",
          "datasource": "mydb"
        }
        ]
     }
  ]
}

What I currently use is

sed 's/"datasource": null/"datasource": "mydb"/'

This produces the output I need, but I keep thinking that it is a shame to use sed for this job, when there are tools like jq that can work on JSON in a much better way.

First you need to identify the objects you want to update. Since you want to set the null datasources of the panels to "mydb" , you could do this:

$ jq '.rows[].panels[].datasource //= "mydb"' input.json

If you want to update any datasource property on any object at any level, you could use .. to recursively search for them.

$ jq '(.. | select(objects | has("datasource"))).datasource //= "mydb"' input.json

For the record, it is also quite easy to change all key values that are null (no matter where they occur) to some other value. Versions of jq > 1.5 include a jq-defined filter, walk/1, which can be used (eg by copying and pasting) in other versions of jq as well.

For example, to change all key values that are null to 0 using walk/1:

walk(if type == "object" then with_entries(.value //= 0) else . end)

Here is the jq-provided definition of walk/1:

def walk(f):
  . as $in
  | if type == "object" then
      reduce keys[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
    elif type == "array" then map( walk(f) ) | f
    else f
    end;

Here is a solution which uses tostream , reduce and setpath to set any leaf datasource attribute whose value is null to "mydb" .

reduce (tostream|select(length==2)) as [$p,$v] (
  .
; if $p[-1] == "datasource" and $v == null then setpath($p; "mydb") else . end
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM