How to abbreviate/truncate the value of every key? - jq

I have a JSON like:
{
"a": "hi",
"b": 4213,
"c": 23154646789132456,
"d": "a very long string that should be shortened",
"e": {
"x": "some value",
"y": {
"alpha": "foo"
}
}
}
I would like to use jq to "abbreviate" values longer than n characters. Each value should be converted to a string s, and if the result is longer than n, it should be replaced with s[:n] + " ...". If the string is not too long, it should ideally be left as the original value. I expect a result like:
# n=5
{
"a": "hi",
"b": 4213, # "4213" would be acceptable too, but not preferable
"c": "23154 ...",
"d" : "a ver ...,
"e" : "{\n\"x\" ..." # I don't care how whitespace is handled
}
# yes, I know there's no comments in JSON :)
The idea is to have something like "folding" when looking at complex objects, so that I can get an overview of what top level keys are there, and then decide which key I want to "zoom in" to (eg jq '.e').
In Python I could do something like:
j = load_my_json()
for k in j:
s = str(j[k])
if len(s) > n:
j[k] = f"{s[:10]}..."
But how can I do it in jq?

After some trial and error, I came up with:
$ cat j.json | jq 'to_entries | map({(.key): (.value | tostring[:20] + " ...")})' | jq -s '.[] | add'
{
"a": "shortval",
"b": "4213",
"c": "23154646789132456",
"d": "a very long string t",
"e": "{\"x\":\"some value\",\"y"
}
Which is what I want. This seems a bit inelegant, especially having to do the -s call, but it's good enough for what I'm doing. I'll leave the question unanswered in case someone has a better solution.

In the simplest case, we convert each item using tostring and check the string's length
$ jq --argjson n 10 '.[] |= (
tostring | if length > $n then .[:$n] + "..." else . end
)' j.json
{
"a": "hi",
"b": "4213",
"c": "2315464678...",
"d": "a very lon...",
"e": "{\"x\":\"some..."
}
To prevent the numbers from being converted to strings, we can wrap the simple case with another if condition checking the item's type
$ jq --argjson n 10 '.[] |= (
if type == "number" then . else
tostring | if length > $n then .[:$n] + "..." else . end
end
)' j.json
{
"a": "hi",
"b": 4213,
"c": 23154646789132456,
"d": "a very lon...",
"e": "{\"x\":\"some..."
}
Finally, if we want to keep the numbers as numbers if and only if their string representation matches the length criteria, we serialize both conditions with both applying tostring once for conversion and once for testing:
$ jq --argjson n 10 '.[] |= (
if type == "number" then . else tostring end
| if tostring | length > $n then tostring | .[:$n] + "..." else . end
)' j.json
{
"a": "hi",
"b": 4213,
"c": "2315464678...",
"d": "a very lon...",
"e": "{\"x\":\"some..."
}

Related

Conditionally output a field?

In this example I only want isGreaterThanOne field to be shown if it's true. Here's what I started with (always shown)
echo '[{"a":5},{"a":1}]' | jq '[.[] | {value:.a, isGreaterThanOne:(.a>1)}]'
I inserted an if statement
echo '[{"a":5},{"a":1}]' | jq '[.[] | {value:.a, X:(if .a>1 then "Y" else "N" end) }]'
Then got stuck trying to move the field into the conditional. Also it seems like I must have an else with an if
echo '[{"a":5},{"a":1}]' | jq '[.[] | {value:.a, (if .a>1 then (K:"Y)" else (L:"N") end) }]'
I want the below as the result (doesn't need to be pretty printed)
[
{
"value": 5,
"X": "Y"
},
{
"value": 1,
}
]
Using if, make one branch provide an empty object {} which wouldn't contain the extra field:
map({value: .a} + if .a > 1 then {X: "Y"} else {} end)
Demo
Alternatively, equip only selected items with the extra field:
map({value: .a} | select(.value > 1).X = "Y")
Demo
Output:
[
{
"value": 5,
"X": "Y"
},
{
"value": 1
}
]

JQ how to combine heterogeneous objects into one array?

How could I use JQ to parse the following JSON object and produce the output below?
JSON Input:
{
"key1": {
"a": "A"
},
"key2": {
"b": "123"
},
"key3": {
"c": ["C1", "C2"]
}
}
Desired Output:
[
"a": "A",
"b": 123,
"c": "C1",
"c": "C2"
]
The following program produces the output shown below it:
def q: "\"\(.)\"";
.[]
| to_entries[]
| (.key|q) as $k
| if .value|type == "array"
then .value[] | "\($k): \(q)"
else "\($k): \(.value|q)"
end
Output:
"a": "A"
"b": "123"
"c": "C1"
"c": "C2"
This, or something very much like it, should be sufficient for using in a bash script, but if you really want the format shown in the Q, feel free to fiddle around. A more useful way to spend your time would probably be to read up on jq-bash interoperability, e.g. here on SO:
Is there a way to output jq into multiple variables for bash script?
get field from json and assign to variable in bash script?
... and many others.

regex replacement for whole object tree / reverse operation to `tostring`

So I have big json, where I need to take some subtree and copy it to other place, but with some properties updated (a lot of them). So for example:
{
"items": [
{ "id": 1, "other": "abc"},
{ "id": 2, "other": "def"},
{ "id": 3, "other": "ghi"}
]
}
and say, that i'd like to duplicate record having id == 2, and replace char e in other field with char x using regex. That could go (I'm sure there is a better way, but I'm beginner) something like:
jq '.items |= . + [.[]|select (.id == 2) as $orig | .id=4 | .other=($orig.other | sub("e";"x"))]'<sample.json
producing
{
"items": [
{
"id": 1,
"other": "abc"
},
{
"id": 2,
"other": "def"
},
{
"id": 3,
"other": "ghi"
},
{
"id": 4,
"other": "dxf"
}
]
}
Now that's great. But suppose, that there ins't just one other field. There are multitude of them, and over deep tree. Well I can issue multiple sub operations, but assuming, that replacement pattern is sufficiently selective, maybe we can turn the whole JSON subtree to string (trivial, tostring method) and replace all occurences using singe sub call. But how to turn that substituted string back to — is it call object? — to be able to add it back to items array?
Here's a program that might be a solution to the general problem you are describing, but if not at least illustrates how problems of this type can be solved. Note in particular that there is no explicit reference to a field named "other", and that (thanks to walk) the update function is applied to all candidate JSON objects in the input.
def update($n):
if .items | length > 0
then ((.items[0]|keys_unsorted) - ["id"]) as $keys
| if ($keys | length) == 1
then $keys[0] as $key
| (.items|map(.id) | max + 1) as $newid
| .items |= . + [.[] | select(.id == $n) as $orig | .id=$newid | .[$key]=($orig[$key] | sub("e";"x"))]
else .
end
else .
end;
walk(if type == "object" and has("items") then update(2) else . end)

jq how to merge array objects into single object

in jq its possible to add objects using the + operator
if you have an array
[
{
"a": "value"
},
{
"b": "value"
},
{
"c": "value"
}
]
I want to convert it into a single object { a:"value", b:"value", c:"value" }
I can use the following filter .[0] + .[1] + .[2], but i want to do it for the whole array without specifying all the indexes.
You can use reduce:
reduce .[] as $o ({}; . + $o)
returns:
{
"a": "value",
"b": "value",
"c": "value"
}
The simplest way is just to call add filter.
"The filter add takes as input an array, and produces as output the elements of the array added together. This might mean summed, concatenated or merged depending on the types of the elements of the input array - the rules are the same as those for the + operator (described above)."
Source: https://stedolan.github.io/jq/manual/#add
$ cat test.json
[
{
"a": "value"
},
{
"b": "value"
},
{
"c": "value"
}
]
$ jq 'add' test.json
{
"a": "value",
"b": "value",
"c": "value"
}
As mentioned by peak in the comment, you can even skip wrapping add filter with the quotes:
$ jq add test.json
{
"a": "value",
"b": "value",
"c": "value"
}
In case you want to merge all objects in array recursively (didn't find any similar answer here):
jq 'reduce .[] as $x ({}; . * $x)'

Combine the value from a key with all array entries

I have json input as follows:
[{
"a": "123",
"b": [
"xyz",
"uvw"
]
}, {
"a": "456",
"b": [
"ghi"
]
}]
and I'd like to produce a list where each object's "a" is combined with each element of "b" using a delimiter. Is this possible to do using jq?
123|xyz
123|uvw
456|ghi
You can change the delimiter on the fly if you parameterize it.
$ jq -r --arg delim '|' '.[] | "\(.a)\($delim)\(.b[])"' input.json

Resources