Skip to main content
deleted 19 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This is fixed by extracting the interesting bits and using indices() on them (and at the same time, I'm dropping the creation of the variable $id_list, as it's strictly not needed):

$ jq '. as $list | (map_values(.thing == "3") | indices(true)) as $id_list | $list[$id_list[]$list[.[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null
. as $list | select_indices(.thing == "3") as $id_list | $list[$id_list[]$list[.[] + 1]

This is fixed by extracting the interesting bits and using indices() on them:

$ jq '. as $list | (map_values(.thing == "3") | indices(true)) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null
. as $list | select_indices(.thing == "3") as $id_list | $list[$id_list[] + 1]

This is fixed by extracting the interesting bits and using indices() on them (and at the same time, I'm dropping the creation of the variable $id_list, as it's strictly not needed):

$ jq '. as $list | map_values(.thing == "3") | indices(true) | $list[.[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null
. as $list | select_indices(.thing == "3") | $list[.[] + 1]
added 301 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
$ jq '. as $list | ($list | indices({thing: "3"})) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1"
}
{
  "thing": "A"
}
null

As you may imagine, this becomes a bit unwieldy if the entries in the array entries are bigcomplex and you only care for smaller parts of themthem; it's difficult to generalise. This

This is fixed by extracting the interesting bits and using indices() on them:

$ jq '. as $list | ($list | map_values(.thing == "3") | indices("3"true)) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null

Then you'll do what every programmer would do, which is to abstract away the details into a function:

def select_indices(f): map_values(f) | indices(true);

... and call that instead

. as $list | select_indices(.thing == "3") as $id_list | $list[$id_list[] + 1]
$ jq '. as $list | ($list | indices({thing: "3"})) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1"
}
{
  "thing": "A"
}
null

As you may imagine, this becomes a bit unwieldy if the entries in the array are big and you only care for smaller parts of them. This is fixed by extracting the interesting bits and using indices() on them:

$ jq '. as $list | ($list | map_values(.thing) | indices("3")) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null
$ jq '. as $list | (indices({thing: "3"})) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1"
}
{
  "thing": "A"
}
null

As you may imagine, this becomes a bit unwieldy if the array entries are complex and you only care for smaller parts of them; it's difficult to generalise.

This is fixed by extracting the interesting bits and using indices() on them:

$ jq '. as $list | (map_values(.thing == "3") | indices(true)) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null

Then you'll do what every programmer would do, which is to abstract away the details into a function:

def select_indices(f): map_values(f) | indices(true);

... and call that instead

. as $list | select_indices(.thing == "3") as $id_list | $list[$id_list[] + 1]
added 443 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

As you may imagine, this becomes a bit unwieldy if the entries in the array are big and you only care for smaller parts of them. This is fixed by extracting the interesting bits and using indices() on them:

$ cat file
[
    {"thing":"3", "other bits": "unimportant"},
    {"thing":"1", "other bits": "unimportant"},
    {"thing":"2", "other bits": "unimportant"},
    {"thing":"3", "other bits": "unimportant"},
    {"thing":"A", "other bits": "unimportant"},
    {"thing":"B", "other bits": "unimportant"},
    {"thing":"C", "other bits": "unimportant"},
    {"thing":"3", "other bits": "unimportant"}
]
$ jq '. as $list | ($list | map_values(.thing) | indices("3")) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null

As you may imagine, this becomes a bit unwieldy if the entries in the array are big and you only care for smaller parts of them. This is fixed by extracting the interesting bits and using indices() on them:

$ cat file
[
    {"thing":"3", "other bits": "unimportant"},
    {"thing":"1", "other bits": "unimportant"},
    {"thing":"2", "other bits": "unimportant"},
    {"thing":"3", "other bits": "unimportant"},
    {"thing":"A", "other bits": "unimportant"},
    {"thing":"B", "other bits": "unimportant"},
    {"thing":"C", "other bits": "unimportant"},
    {"thing":"3", "other bits": "unimportant"}
]
$ jq '. as $list | ($list | map_values(.thing) | indices("3")) as $id_list | $list[$id_list[] + 1]' file
{
  "thing": "1",
  "other bits": "unimportant"
}
{
  "thing": "A",
  "other bits": "unimportant"
}
null
added 443 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 7 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 7 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading