Query and alter JSON documents using dasel utility which also supports other data formats like JSON, TOML, YAML, XML, and CSV.

I have used jq to alter JSON documents, but this is a better solution to perform update process.

Installation process

Install dasel utility.

$ go install github.com/tomwright/dasel/cmd/dasel@maste
go: downloading github.com/tomwright/dasel v1.24.1-0.20220318163600-eabe44d3f6c1
go: downloading github.com/spf13/cobra v1.2.1
go: downloading github.com/alecthomas/chroma v0.9.2
go: downloading github.com/clbanning/mxj/v2 v2.3.3-0.20201214204241-e937bdee5a3e
go: downloading github.com/pelletier/go-toml v1.9.5-0.20220105141732-fed146406641
go: downloading golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/Masterminds/sprig/v3 v3.2.2
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/Masterminds/semver/v3 v3.1.1
go: downloading github.com/google/uuid v1.1.2
go: downloading github.com/Masterminds/goutils v1.1.1
go: downloading github.com/huandu/xstrings v1.3.1
go: downloading github.com/imdario/mergo v0.3.11
go: downloading github.com/mitchellh/copystructure v1.0.0
go: downloading github.com/shopspring/decimal v1.2.0
go: downloading github.com/spf13/cast v1.3.1
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading golang.org/x/text v0.3.7
go: downloading github.com/mitchellh/reflectwalk v1.0.0
go: downloading github.com/dlclark/regexp2 v1.4.0
go: downloading github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964

Ensure that it will be included in PATH. Update ~/.bashrc accordingly.

$ export PATH="$PATH:~/go/bin"

Ensure that command is available.

$ dasel
Query and modify data structures using selector strings.

Usage:
  dasel [command]

Available Commands:
  completion  generate the autocompletion script for the specified shell
  delete      Delete properties from the given file.
  help        Help about any command
  put         Update properties in the given file.
  select      Select properties from the given file.
  update      Update dasel to the latest stable release.

Flags:
  -h, --help      help for dasel
  -v, --version   version for dasel

Use "dasel [command] --help" for more information about a command.

Inspect sample file that will be used in the following examples.

$ cat widget.json
{
  "widget": {
    "debug": false,
    "image": {
      "alignment": "center",
      "comment": "Sun image",
      "hOffset": 250,
      "name": "sun1",
      "src": "Images/Sun.png",
      "vOffset": 250
    },
    "text": {
      "alignment": "center",
      "data": "Click Here",
      "hOffset": 250,
      "name": "text1",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;",
      "size": 38,
      "style": "bold",
      "vOffset": 100
    },
    "window": {
      "height": 500,
      "name": "main_window",
      "title": "Sample Konfabulator Widget",
      "width": 500
    }
  }
}

Conversion between data formats

Convert between multiple data formats.

$ dasel --read json --write yaml --file widget.json | tee widget.yaml
widget:
  debug: false
  image:
    alignment: center
    comment: Sun image
    hOffset: 250
    name: sun1
    src: Images/Sun.png
    vOffset: 250
  text:
    alignment: center
    data: Click Here
    hOffset: 250
    name: text1
    onMouseUp: sun1.opacity = (sun1.opacity / 100) * 90;
    size: 38
    style: bold
    vOffset: 100
  window:
    height: 500
    name: main_window
    title: Sample Konfabulator Widget
    width: 500
$ dasel --read json --write xml --file widget.json | tee widget.xml
<widget>
  <debug>false</debug>
  <image>
    <alignment>center</alignment>
    <comment>Sun image</comment>
    <hOffset>250</hOffset>
    <name>sun1</name>
    <src>Images/Sun.png</src>
    <vOffset>250</vOffset>
  </image>
  <text>
    <alignment>center</alignment>
    <data>Click Here</data>
    <hOffset>250</hOffset>
    <name>text1</name>
    <onMouseUp>sun1.opacity = (sun1.opacity / 100) * 90;</onMouseUp>
    <size>38</size>
    <style>bold</style>
    <vOffset>100</vOffset>
  </text>
  <window>
    <height>500</height>
    <name>main_window</name>
    <title>Sample Konfabulator Widget</title>
    <width>500</width>
  </window>
</widget>
$ dasel --read xml --write toml --file widget.xml | tee widget.toml
[widget]
  debug = "false"

  [widget.image]
    alignment = "center"
    comment = "Sun image"
    hOffset = "250"
    name = "sun1"
    src = "Images/Sun.png"
    vOffset = "250"

  [widget.text]
    alignment = "center"
    data = "Click Here"
    hOffset = "250"
    name = "text1"
    onMouseUp = "sun1.opacity = (sun1.opacity / 100) * 90;"
    size = "38"
    style = "bold"
    vOffset = "100"

  [widget.window]
    height = "500"
    name = "main_window"
    title = "Sample Konfabulator Widget"
    width = "500

$ dasel --read toml --write json --file widget.toml 
{
  "widget": {
    "debug": "false",
    "image": {
      "alignment": "center",
      "comment": "Sun image",
      "hOffset": "250",
      "name": "sun1",
      "src": "Images/Sun.png",
      "vOffset": "250"
    },
    "text": {
      "alignment": "center",
      "data": "Click Here",
      "hOffset": "250",
      "name": "text1",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;",
      "size": "38",
      "style": "bold",
      "vOffset": "100"
    },
    "window": {
      "height": "500",
      "name": "main_window",
      "title": "Sample Konfabulator Widget",
      "width": "500"
    }
  }
}

Compact a JSON document.

$ dasel --compact --file widget.json | tee widget.compact.json
{"widget":{"debug":false,"image":{"alignment":"center","comment":"Sun image","hOffset":250,"name":"sun1","src":"Images/Sun.png","vOffset":250},"text":{"alignment":"center","data":"Click Here","hOffset":250,"name":"text1","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;","size":38,"style":"bold","vOffset":100},"window":{"height":500,"name":"main_window","title":"Sample Konfabulator Widget","width":500}}}

Select specific data

Read help information.

$ dasel select --help
Select properties from the given file.

Usage:
  dasel select -f <file> -p <json,yaml> -s <selector> [flags]

Flags:
      --color                   Alias of --colour.
      --colour                  Print colourised output.
  -c, --compact                 Compact the output by removing all pretty-printing where possible.
      --escape-html             Escape HTML tags when writing output.
  -f, --file string             The file to query.
      --format string           Formatting template to use when writing results.
  -h, --help                    help for select
      --length                  Output the length of the selected value.
      --merge-input-documents   Merge multiple input documents into an array.
  -m, --multiple                Select multiple results.
  -n, --null                    Output null instead of value not found errors.
  -p, --parser string           Shorthand for -r FORMAT -w FORMAT.
      --plain                   Alias of -w plain
  -r, --read string             The parser to use when reading.
  -s, --selector string         The selector to use when querying the data structure.
  -w, --write string            The parser to use when writing.

Select a single value.

$ dasel select –file widget.json –plain “.widget.debug”
false

Select a single object.

$ dasel select --file widget.json  ".widget.(name=main_window)"
{
  "height": 500,
  "name": "main_window",
  "title": "Sample Konfabulator Widget",
  "width": 500
}
$ dasel select --file widget.json --plain  ".widget.(name=main_window)"
map[height:500 name:main_window title:Sample Konfabulator Widget width:500]

Create or update data

Read help information.

$ dasel put --help
Update properties in the given file.

Usage:
  dasel put [command]

Available Commands:
  bool        Update a bool property in the given document.
  document    Put an entire document into the given document.
  int         Update an int property in the given document.
  object      Put an object in the given document.
  string      Update a string property in the given document.

Flags:
  -c, --compact                 Compact the output by removing all pretty-printing where possible.
      --escape-html             Escape HTML tags when writing output.
  -f, --file string             The file to query.
  -h, --help                    help for put
      --merge-input-documents   Merge multiple input documents into an array.
  -m, --multiple                Select multiple results.
  -o, --out string              Output destination.
  -p, --parser string           Shorthand for -r FORMAT -w FORMAT.
  -r, --read string             The parser to use when reading.
  -s, --selector string         The selector to use when querying the data structure.
  -v, --value string            Value to put.
  -w, --write string            The parser to use when writing.

Use "dasel put [command] --help" for more information about a command.

Update a single key.

$ dasel put bool --file widget.json .widget.debug true
$ dasel select --file widget.json --plain .widget.debug
true

Add an object.

$ dasel put object --file widget.json -t bool -t string -t int  ".widget.loading" show=true title=Loading size=12
$ cat widget.json 
{
  "widget": {
    "debug": true,
    "image": {
      "alignment": "center",
      "comment": "Sun image",
      "hOffset": 250,
      "name": "sun1",
      "src": "Images/Sun.png",
      "vOffset": 250
    },
    "loading": {
      "show": true,
      "size": 12,
      "title": "Loading"
    },
    "text": {
      "alignment": "center",
      "data": "Click Here",
      "hOffset": 250,
      "name": "text1",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;",
      "size": 38,
      "style": "bold",
      "vOffset": 100
    },
    "window": {
      "height": 500,
      "name": "main_window",
      "title": "Sample Konfabulator Widget",
      "width": 500
    }
  }
}

Delete specific data

Read help information.

$ dasel delete --help
Delete properties from the given file.

Usage:
  dasel delete -f <file> -p <json,yaml> -s <selector> [flags]

Flags:
  -c, --compact                 Compact the output by removing all pretty-printing where possible.
      --escape-html             Escape HTML tags when writing output.
  -f, --file string             The file to delete from.
  -h, --help                    help for delete
      --merge-input-documents   Merge multiple input documents into an array.
  -m, --multiple                Delete multiple results.
  -o, --out string              Output destination.
  -p, --parser string           Shorthand for -r FORMAT -w FORMAT.
      --plain                   Alias of -w plain
  -r, --read string             The parser to use when reading.
  -s, --selector string         The selector to use when deleting from the data structure.
  -w, --write string            The parser to use when writing.

Delete an object.

➜ dasel delete --file widget.json ".widget.loading"
➜ cat widget.json 
{
  "widget": {
    "debug": true,
    "image": {
      "alignment": "center",
      "comment": "Sun image",
      "hOffset": 250,
      "name": "sun1",
      "src": "Images/Sun.png",
      "vOffset": 250
    },
    "text": {
      "alignment": "center",
      "data": "Click Here",
      "hOffset": 250,
      "name": "text1",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;",
      "size": 38,
      "style": "bold",
      "vOffset": 100
    },
    "window": {
      "height": 500,
      "name": "main_window",
      "title": "Sample Konfabulator Widget",
      "width": 500
    }
  }
}

Additional information

Read the dasel documentation as there is more see and do.

ko-fi