问题
I have a file of json that includes six 'invoice' object, each object is 140+ lines of values. I want to see only a few values for each of the six objects. jq
seemed like a promising solution.
I'm using jq version 1.5-1-a5b5cbe
which was available via apt
's usual repositories.
I can do cat stripe-invoices-list.json | jq -C '. | {invoice_id: .data[].id,}'
and it returns:
{
"invoice_id": "in_1Gq39HFGUwFHXzvlUOGG3Rv4"
}
{
"invoice_id": "in_1GpyM1FGUwFHXzvlio9pfaM9"
}
{
"invoice_id": "in_1GpyHUFGUwFHXzvlHDS727su"
}
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5"
}
That makes sense and works properly. Those are the id's for the six invoice objects. All is good, though just seeing the id's is not helpful. So...
I try to add more data to the output but it then creates redundant and even incorrect output:
$ cat stripe-invoices-list.json | jq -C '. | {invoice_id: .data[].id, client: .data[].customer_name,}'
results in:
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp",
"client": "client_two"
}
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp",
"client": "client_three"
}
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp",
"client": "client_four"
}
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp",
"client": "client_five"
}
{
"invoice_id": "in_1Gpy4HFGUwFHXzvl6k82godp",
"client": "client_one"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_one"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_two"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_three"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_four"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_five"
}
{
"invoice_id": "in_1GpjNjFGUwFHXzvl880gp0o9",
"client": "client_one"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_one"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_two"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_three"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_four"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_five"
}
{
"invoice_id": "in_1GSkk7FGUwFHXzvlfWdhcad5",
"client": "client_one"
which is giving redundant and scrambled information. It's redundant because in_1GSkk7FGUwFHXzvlfWdhcad5
is listed multiple times, and scrambled because that one invoice is being associated with every client.
When I open the stripe-invoices-list.json
file in vim I can see that invoice id in_1GSkk7FGUwFHXzvlfWdhcad5
is associated with client_one
.
Can anyone suggest what could be causing this problem, or any debugging steps I could take?
How do I get jq
to print this properly?
回答1:
This is a common pitfall, you're expanding data
twice in the same object constructor, and it leads to a combinatorial explosion. Below is the correct way of doing it.
.data[] | {invoice_id: .id, client: .customer_name}
来源:https://stackoverflow.com/questions/62198965/why-does-jq-print-records-redundantly-and-mismatched-and-how-to-fix-it