Dasel: Difference between revisions

From wikinotes
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
Inspired by [[jq]] and [[yq]], but operates on [[json]], [[yaml]], [[toml]], [[xml]], [[csv]].<br>
Inspired by [[jq]] and [[yq]], but operates on [[json]], [[yaml]], [[toml]], [[xml]], [[csv]].<br>
Totally standalone, no runtime dependencies.
Totally standalone, no runtime dependencies.
I'm not certain you can append to a list,


= Documentation =
= Documentation =
Line 21: Line 23:
     '.category.three'  '3'
     '.category.three'  '3'


# print 'foo.yml',  
# print 'foo.yml',
#  with obj['category']['three'] = "3" added,
#  with obj['category']['three'] = "3" added,
#  while also preserving comments
#  while also preserving comments
Line 27: Line 29:
# see: https://stackoverflow.com/questions/14282617/hunk-1-failed-at-1-whats-that-mean
# see: https://stackoverflow.com/questions/14282617/hunk-1-failed-at-1-whats-that-mean
diff \
diff \
     <(dasel -r yaml -f foo.yml) \
     <(dasel -f foo.yml) \
     <(dasel put string -r yaml -f foo.yml -o -) \
     <(dasel put string -f foo.yml -o - '.category.three' '3') \
     | patch -o - foo.yml
     | patch -o - foo.yml
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Examples -->
</blockquote><!-- Examples -->
= Datatypes =
<blockquote>
<syntaxhighlight lang="yaml">
string:    string
int:      integer
bool:      true/false
object:    a set of key/value pairs (can be assigned to objects of various types)
document:  a json/yaml/toml/... object (can be put into documents of other types)
</syntaxhighlight>
</blockquote><!-- Datatypes -->
= File Formats =
<blockquote>
== CSV ==
<blockquote>
cat out.csv | dasel -p csv -m '.-'
</blockquote><!-- CSV -->
== XML ==
<blockquote>
See https://github.com/TomWright/dasel-docs/blob/master/examples/xml.md
Query
<syntaxhighlight lang="bash">
# COUNT number of matching elements
cat config.xml \
  | dasel -r xml '.configuration.device.len()'
# QUERY property value (ex. <device id="foo">)
cat config.xml \
  | dasel -r xml '.configuration.device.[0].-id'
# FILTER devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin))'
# COUNT devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin)).count()'
</syntaxhighlight>
Mutation
<syntaxhighlight lang="bash">
# REPLACE the contents of the LAST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
  '.configuration.device'
# REPLACE the contents of the FIRST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
    '.configuration.device.[0]'
# REPLACE the contents of the MATCHING element
cat config.xml \
  | dasel put -r xml -v '<foo></foo>' \
    '.configuration.device.all().filter(equal(-name,mordin))'
# APPEND a new element
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
  '.configuration.device.append()'
</syntaxhighlight>
</blockquote><!-- XML -->
</blockquote><!-- File Formats -->

Latest revision as of 05:29, 29 January 2023

Inspired by jq and yq, but operates on json, yaml, toml, xml, csv.
Totally standalone, no runtime dependencies.

I'm not certain you can append to a list,

Documentation

official docs https://daseldocs.tomwright.me/
github https://github.com/TomWright/dasel

Examples

# modify 'foo.yml', adding obj['category']['three'] = "3"
dasel put string \
    -f foo.yml \
    '.category.three'  '3'

# print 'foo.yml',
#   with obj['category']['three'] = "3" added,
#   while also preserving comments
#
# see: https://stackoverflow.com/questions/14282617/hunk-1-failed-at-1-whats-that-mean
diff \
    <(dasel -f foo.yml) \
    <(dasel put string -f foo.yml -o - '.category.three' '3') \
    | patch -o - foo.yml

Datatypes

string:    string
int:       integer
bool:      true/false
object:    a set of key/value pairs (can be assigned to objects of various types)
document:  a json/yaml/toml/... object (can be put into documents of other types)

File Formats

CSV

cat out.csv | dasel -p csv -m '.-'

XML

See https://github.com/TomWright/dasel-docs/blob/master/examples/xml.md

Query

# COUNT number of matching elements
cat config.xml \
  | dasel -r xml '.configuration.device.len()'

# QUERY property value (ex. <device id="foo">)
cat config.xml \
  | dasel -r xml '.configuration.device.[0].-id'

# FILTER devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin))'

# COUNT devices, with a 'name' property, that equals 'mordin'
# (ex. <device name="mordin"></device>...<device name="foo"></device>
cat config.xml \
  | dasel -r xml '.configuration.device.all().filter(equal(-name,mordin)).count()'

Mutation

# REPLACE the contents of the LAST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
   '.configuration.device'

# REPLACE the contents of the FIRST
# <configuration><device>...</device></configuration> with <a>1</a>
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
    '.configuration.device.[0]'

# REPLACE the contents of the MATCHING element
cat config.xml \
  | dasel put -r xml -v '<foo></foo>' \
    '.configuration.device.all().filter(equal(-name,mordin))'

# APPEND a new element
cat config.xml \
  | dasel put -r xml -v '<a>1</a>' \
   '.configuration.device.append()'