summaryrefslogtreecommitdiffhomepage
path: root/jq.html.markdown
diff options
context:
space:
mode:
authorJack Kuan <kjkuan@gmail.com>2022-06-03 01:00:10 -0400
committerJack Kuan <kjkuan@gmail.com>2022-06-03 01:00:10 -0400
commit2257d6bae2960c436295ce7c6da45151ea7a7d3d (patch)
treee4d58f6911b8ad99c9da684e50a5f764bbaa52e8 /jq.html.markdown
parentee7c1918b47774c4534d9ac3fe450ffc662b8b0e (diff)
Fix typo and talk about updating and deleting.
Diffstat (limited to 'jq.html.markdown')
-rw-r--r--jq.html.markdown34
1 files changed, 30 insertions, 4 deletions
diff --git a/jq.html.markdown b/jq.html.markdown
index 2178820f..3691baaf 100644
--- a/jq.html.markdown
+++ b/jq.html.markdown
@@ -107,7 +107,7 @@ jq . hello.json
# Below, `val` is the variable name to bind the value, `123`, to.
# The variable is then referenced as `$val`.
#
-jq --arg val 123 -n '$val' # $val is the string "123" here
+jq -n --arg val 123 '$val' # $val is the string "123" here
# Output:
# "123"
@@ -115,7 +115,7 @@ jq --arg val 123 -n '$val' # $val is the string "123" here
# If you need to pass a JSON value, use `--argjson`
#
-jq --arg val 123 -n '$val' # $val is a number
+jq -n --argjson val 123 '$val' # $val is a number
# Output:
# 123
@@ -773,9 +773,33 @@ jq -n '.a=1, .a.b=2' # => {"a": 1} {"a": {"b": 2}}
# nested objects.
+# In addition to the assignment operator, jq also has operators like:
+# `+=`, `-=`, `*=`, and '/=', ... etc. Basically, `a op= b` is a shorthand
+# for `a = a op b`, and they are handy for updating an object attribute or
+# an item in an array based on its current value. Examples:
+#
+jq -n '.a.b.c = 3 | .a.b.c = .a.b.c + 1' # => {"a": {"b": {"c": 4}}}
+jq -n '.a.b.c = 3 | .a.b.c += 1' # => {"a": {"b": {"c": 4}}}
+
+
+# To delete a value, use `del/1`, which takes a path expression that specifies
+# the locations of the things to be deleted. Example:
+#
+jq -n '{a: 1, b: {c: 2}, d: [3, 4, 5]} | del(.b.c, .d[1]) | .b.x = 6'
+
+# Output:
+# {
+# "a": 1,
+# "b": {
+# "x": 6
+# },
+# "d": [
+# 3,
+# 5
+# ]
+# }
+
# FIXME: Cover more topics
-# - update
-# - deletion
# - function definition
# - ...
```
@@ -783,3 +807,5 @@ jq -n '.a=1, .a.b=2' # => {"a": 1} {"a": {"b": 2}}
## Further Reading
- https://stedolan.github.io/jq/manual/
- https://github.com/stedolan/jq/wiki/jq-Language-Description
+- https://github.com/stedolan/jq/wiki/Cookbook
+- https://github.com/stedolan/jq/blob/master/src/builtin.jq