2

I am trying to get this:

[   
    ["John Black",[ 
        ["Lorem ipsum dolor sit amet.",27], 
        ["Ut tempus lectus ut mi.",23]
    ]],
    ["Peter Pan",[ 
        ["Quisque pulvinar odio.",22],
        ["Nec ut lorem quis interdum elit.",32]
    ]],
    ["Gary Halbert",[ 
        ["Placerat aliquam.",17]
    ]],
    ["Richard Gere",[ 
        ["Porttitor commodo fermentum.",28]
    ]]
]

Till now, this is what I got:

export A=$(cat <<'EOL'
[
["John Black",["Lorem ipsum dolor sit amet.",27]],
["Peter Pan",["Quisque pulvinar odio.",22]],
["John Black",["Ut tempus lectus ut mi.",23]],
["Gary Halbert",["Placerat aliquam.",17]],
["Peter Pan",["Nec ut lorem quis interdum elit.",32]],
["Richard Gere",["Porttitor commodo fermentum.",28]]
]
EOL
)
echo "$A" | jq 'map({(.[0]): .[1]}) | add'

Resulting this:

{
  "John Black": [
    "Ut tempus lectus ut mi.",
    23
  ],
  "Peter Pan": [
    "Nec ut lorem quis interdum elit.",
    32
  ],
  "Gary Halbert": [
    "Placerat aliquam.",
    17
  ],
  "Richard Gere": [
    "Porttitor commodo fermentum.",
    28
  ]
}

I am using jq-1.5. Any ideas? Thanks.

3
  • 2
    Aside: export is utterly unneeded here; it's using a limited resource unnecessarily (environment variables are stored in the same space used for the argument vector, so when you export a shell variable you're decreasing your maximum command-line length for all subprocesses of that shell). Commented Mar 6, 2017 at 21:11
  • Man, where did you learn that? If that was a book, please, I want that too... Commented Mar 6, 2017 at 22:46
  • 2
    @Roger: See in-ulm.de/~mascheck/various/argmax Commented Mar 6, 2017 at 23:16

2 Answers 2

2

This is an appropriate use case for a reducer. Most of the below is related not to joining items under shared keys, but to getting them into the desired nested-list form:

jq -n '[
  inputs |
  reduce .[] as $item ({}; .[$item[0]] += [$item[1]]) |
  to_entries |
  .[] |
  [.key, .value]
]' <<<"$A"

...yields as output (edited only for compactness with regard to whitespace):

[
  ["John Black", [["Lorem ipsum dolor sit amet.", 27], ["Ut tempus lectus ut mi.",23]]],
  ["Peter Pan", [["Quisque pulvinar odio.",22], ["Nec ut lorem quis interdum elit.", 32]]],
  ["Gary Halbert", [["Placerat aliquam.", 17]]],
  ["Richard Gere", [["Porttitor commodo fermentum.", 28]]]
]
Sign up to request clarification or add additional context in comments.

2 Comments

If there is a book or a course about this, I want to know!
@Roger: You won't find such advanced techniques in the tutorial, but the manual covers everything - albeit in a condensed form that may not make the possibilities obvious. The manual does have examples, however, and there's also an online playground. Finally, there are plenty of excellent answers on SO already (this one definitely qualifies).
0

the Couchdb engine is append only, it means that each document will append into a file manager by Couchdb, and each file is related to a db.

to use properly think how to avoid updates in the same document, remember all revisions will be storage.

my suggestion for each document.

{
  "name": "John Black",
  "entries": [
    {
      "test": "Lorem ipsum dolor sit amet.",
      "value": 27
    }, 
    {
       "text": "Ut tempus lectus ut mi.",
       "value": 23
    }
  ],
  "type": "user"
}

4 Comments

So, with CouchDB, if I need to add other quotes to an author I will have to read the JSON object/array, make the addition and overwrite the entire document?
yes and this will generate a revision, which is a copy of the document.
Did a prior rev of the question reference CouchDB? (As the current version asks only about jq, I'm curious to see this here).
The question was too large so I edited it. As far as I could see, I did not had too much to gain, in my case, working with CouchDB. Indeed, I decided it would be quicker to save the JSON array into a gunziped text file and read it back JQ to further editions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.