Skip to content

Extracting a json subtree for processing (without preprocessing with jq) #1512

Answered by johnkerl
smammy asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @smammy !

jq is perhaps a bit more natural here, as it puts maps (objects) and arrays (lists) on equal footing. So jq .a test.json is fine whether the value at the "a" key is a scalar, list, or object.

Miller is a record processor -- it takes as Millerable JSON input (see also here:

  • A single object
  • A list of objects

This means that the most intuitive idea does not work:

$ mlr -j --from test.json put '$* = $a'
Cannot assign non-map to existing map; got array.

A second optional (not bonus-level concise IMO) is

$ mlr -j --from test.json put -q '@records = $a; end { emit @records }'
[
{
  "records": [
    {
      "x": 1,
      "y": 2,
      "z": 3
    },
    {
      "x": 4,
      "y": 5…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@smammy
Comment options

Answer selected by smammy
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants