You can use jq '.[] | .login, .id' to obtain each login followed by its id.
Using jq, extract fields and subfields from a list of objects, grouping paired subfields for saving to csv - Unix & Linux Stack Exchange
How do I select multiple keys for output?
how to use jq to extract multiple text" node which are in a big nested json
Is it possible to output multiple values on a single line?
You want to run a .context,.score filter on each element of v I think:
$ jq -r '.[] | [.c, .e, .score, (.v[] | .context,.score)] | @csv' file.json
"A","B",0.99,"asdf",0.98,"bcdfd",0.97
This is equivalent to using the builtin map function without assembling the results back into an array.
The following creates a JSON-encoded CSV record for each top-level array element, and then extracts and decodes them. For each of the top-level elements, the values of the sub-array is incorporated by "flattening" the array.
jq -r 'map([ .c,.e,.score, (.v|map([.context, .score])) ] | flatten | @csv)[]' file
Given a test document equivalent of the following:
[
{
"c": "A",
"e": "B",
"score": 0.99,
"v": [
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "bcdfd", "score": 0.97, "url": "..." }
]
},
{
"c": "A",
"e": "B",
"score": 0.99,
"v": [
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "bcdfd", "score": 0.97, "url": "..." }
]
},
{
"c": "A",
"e": "B",
"score": 0.99,
"v": [
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "asdf", "score": 0.98, "url": "..." },
{ "context": "bcdfd", "score": 0.97, "url": "..." }
]
}
]
... we get
"A","B",0.99,"asdf",0.98,"bcdfd",0.97
"A","B",0.99,"asdf",0.98,"asdf",0.98,"bcdfd",0.97
"A","B",0.99,"asdf",0.98,"asdf",0.98,"asdf",0.98,"bcdfd",0.97
One could also reorder the operations so that a single use of the @csv operator gets a set of arrays (rather than repeatedly using @csv on single arrays):
jq -r 'map([ .c,.e,.score, (.v|map([.context, .score])) ] | flatten)[]|@csv' file
User can fill out a textarea in form, and the json behind the scene is like this.What user entered is in "text" fields, (for example text = sample text 1), and there are multiple in this case. and it's dynamic. In other word, next day when user enter a lot of information, it will have much more "text" field in json.
#/comments/0/body/content/0/content/0/text
#/comments/0/body/content/1/content/0/content/0/content/0/text
#/comments/0/body/content/1/content/0/content/1/content/0/content/0/content/0/text
I want to extract these word out, how can I do it using jq?
{
"content": [
{
"type": "paragraph",
"content": [
{
"type": "mention",
"attrs": {
"id": "123",
"text": "sample text 1",
"accessLevel": ""
}
},
{
"type": "text",
"text": " sample text 2."
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "mention",
"attrs": {
"id": "456",
"text": "sample text 3",
"accessLevel": ""
}
},
{
"type": "text",
"text": " "
},
{
"type": "mention",
"attrs": {
"id": "789",
"text": "sample text 4",
"accessLevel": ""
}
},
{
"type": "text",
"text": " "
}
]
}
]
}