I have a json of this type:
{
"_index": "indexeer",
"_type": "_doc",
"_id": "3233233233",
"_score": 1,
"_source": {
"Bibtex": {
"Article": {
"AuthorList": [
{
"Affiliation": {
"Affiliation": "Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American Society. [email protected]."
}
}
]
}
}
}
}
I get the Affiliation
using jq
like so:
jq -r '._source.Bibtex.Article.AuthorList[]? | .Affiliation.Affiliation | .[0:rindex(" Electronic address:")]' results.json
It works great, and gives me excatly the affiliations I need.
However, I now need the id
field aswell and produce a csv like so:
"3233233233", "Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American Society"
I am able to get the ID seperately, easily like so:
jq -r '._id' results.json
but, how do I combine the two queries to produce a single output consisting of id
and affiliation
?
Essentially I want to combine the two queries.
CodePudding user response:
Save the id
as a variable using ._id as $id
, then you can use $id
where ever you like:
._id as $id | ._source.Bibtex.Article.AuthorList[].Affiliation.Affiliation | [ $id, .[0:rindex(" Electronic address:")] ] | @csv
Output:
"3233233233","Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American Society. [email protected]."
JqPlay Demo
CodePudding user response:
Use []
to construct an array from your two elements and @csv
to convert to CSV:
jq -r '[
._id,
._source.Bibtex.Article.AuthorList[]?.Affiliation.Affiliation[:rindex(" Electronic address:")]
] | @csv'
Output:
"3233233233","Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American Society. [email protected]."
Multiple authors will be added as columns to your CSV. If you want a single line per author (with the id duplicated for each line), go with 0stone0's approach