INDEX builtin with input - jq

I'm trying to build an INDEX from a json file which contains a list of flat objects.
If I use the construct
jq 'INDEX(.a + ":" + .b)' < data.json
I get what i want. But if I use
jq 'INDEX(input; .a + ":" + .b) data.json
then I get the error
jq: error (at data.json:<last-line>): break
The reason I need to use the second form is that I will then need to JOIN another input to the index of the first, and I don't see how to do that from STDIN.
Any ideas how I can get around this?
(Currently running jq-1.6 on MacOS 10.13.6)
Adding input (data.json):
[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
]
And expected output
{
"a1:b1": {
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
"a2:b2": {
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
"a3:b3": {
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
}

The first argument of INDEX/2 should be a stream of the items to be indexed; here, .[] is exactly what we want:
INDEX(.[]; .a + ":" + .b)

For jq version 1.5 or older, INDEX does not seem to be working (does not exist?), but this works instead:
jq 'map({key: (.a + ":" + .b), value: .}) | from_entries' data.json

Related

How to abbreviate/truncate the value of every key?

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..."
}

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.

How to group by parent and collect all property values of child in gremlin?

I want to collect all shows and their associated genres together. GENRES are child relationship of SHOWS
Sample gemlin graph
So that the output is something similar to:
"1" [a,b]
"2" [c,d]
Sample graph: https://gremlify.com/x8i8stszn2
You can accomplish this using the project() step within Gremlin like this:
g.V("2789").out('WATCHED').hasLabel('SHOW').
project('show', 'genre').
by('NAME').
by(out('HAS_GENRE').values('NAME').fold())
This will return your data formatted like this this:
[
{
"show": 1,
"genre": [
"a",
"b"
]
},
{
"show": 2,
"genre": [
"c",
"d"
]
}
]

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