Search

Dark theme | Light theme
Showing posts with label jq:Joy. Show all posts
Showing posts with label jq:Joy. Show all posts

October 23, 2023

jq Joy: Using String Interpolation

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. With jq we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression is enclosed by parentheses and the first parenthesis is prefixed with a backslash: \(<expression>). The expression can be any valid jq expression and the result of the expression will be inserted into the string.

In the following example we use string interpolation to print a greeting message and we use a simple property reference, but also a bit more complicated expressions:

$ jq --raw-output --null-input '{"prefix": "Hi", "name": "mrhaki", "visitors": 41} | "\(.prefix),\nwelcome \(.name | ascii_upcase)\nYou are visitor \(.visitors + 1)!"'
Hi,
welcome MRHAKI
You are visitor 42!

Written with jq 1.7.

October 5, 2023

jq Joy: Reverse An Array

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. To simply reverse the order of elements in an array we can use the reverse function. The values in the input array are reversed and returned in the output array.

In the following example we use the reverse function for an array:

$ jq --null-input --compact-output '[1, 2, 3, 4, 5] | reverse'
[5,4,3,2,1]

Written with jq 1.7.

October 4, 2023

jq Joy: Checking String Ends Or Starts With Given String

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. We can check if a string starts or ends with a given string using the startswith and endswith functions. We pass the string value we want to see a string value starts with or ends with as the argument to the functions. The function returns true if the string starts or ends with the given string and false otherwise.

In the following examples we use both functions to check some string values in an array:

$ jq --null-input '["mrhaki", "hubert", "MrHaki"] | [.[] | startswith("mr") or startswith("Mr")]'
[
    true,
    false,
    true
]
$ jq --null-input '["jq!", "JSON"] | [.[] | endswith("!")]'
[
  true,
  false
]

Written with jq 1.7.

October 3, 2023

jq Joy: Sum Of Elements In An Array Or Object

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. One of the functions is add which adds all elements in an array or values in an object. The function has no arguments. The elements in an array are added together if they are numbers and concatenated if they are strings. If the input is an object then the values are added together. When the input is an empty array or object then null is returned.

In the following examples we see different usages of the add function:

$ jq --null-input '[1, 2, 3, 4,] | add'
10
$ jq --null-input '["a", "b", "c", "d"] | add'
"abcd"
$ jq --null-input '{ "burger": 3.23, "drinks": 2.89 } | add'
6.12
$ jq --null-input '[] | add'
null

Written with jq 1.7.

jq Joy: Flatten Arrays

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. We can use the flatten function to flatten nested arrays into a single array. If we use the function without an argument the arrays are flattened recursively, resulting in a flat array with all elements. But we can also set the depth we want to flatten the array by passing an integer argument to the flatten function.

In the following example we use the flatten function without arguments to recursively flatten the array:

$ jq --null-input '[1, [2, 3], [[4]], 5] | flatten'
[
  1,
  2,
  3,
  4,
  5
]

We can pass an argument to the flatten function to indicate the depth to flatten the collection. In the following example we want to flatten only one level deep:

$ jq --null-input '[1, [2, 3], [[4]], 5] | flatten(1)'
[
  1,
  2,
  3,
  [
    4
  ],
  5
]

Written with jq 1.7.

September 26, 2023

jq Joy: Deleting Keys From An Object

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. If we want to delete keys from an object we can use the del function and pass the key name as argument. The argument is actually a path expression so we can refer to a key name with .key, but also use jq expressions.

In the following examples we use the del function in several use cases:

$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
} | del(.username)'
{
  "firstName": "Hubert",
  "lastName": "Klein Ikkink"
}
$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
} | del(.username) | del(.lastName)'
{
  "firstName": "Hubert"
}

If we want to remove multiple keys at once we can pass multiple key names separated by comma to the del function:

$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
} | del(.username, .lastName)'
{
  "firstName": "Hubert"
}

We can use also jq path expressions for example to delete keys by index instead of key name:

$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
    } | del(.[keys_unsorted | .[0,2]])'
{
  "firstName": "Hubert"
}

Written with jq 1.7.

jq Joy: Getting Keys From Object And Indices From Array

jq is a powerful tool to work with JSON from the command-line. The tool has a lot of functions that makes our live easier. For example we can use the keys and keys_unsorted functions to get the keys from an object. The function keys will return the keys in sorted order while keys_unsorted will return them in the original order from the object. With the same functions we can also get the indices of the elements in an array, but there is no sorting involved, so both functions return the same output.

In the following examples we first use keys and then keys_unsorted to get the keys from an object:

$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
} | keys'
[
  "firstName",
  "lastName",
  "username"
]
$ jq --null-input '{
    "username": "mrhaki",
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
} | keys_unsorted'
[
  "username",
  "firstName",
  "lastName"
]

To get the indices of an array we can use the keys function as well:

$ jq --null-input '[1, 10, 100] | keys'
[
  0,
  1,
  2
]

Written with jq 1.7.