Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq cannot be tsv-formatted, only array error

Tags:

json

csv

jq

I've this structure

file.json:

{
"base_price_mw": 249.99, 
"best_offer_base_price": 280.06, 
"best_offer_nature": 11, 
"best_offer_promo_price": 247.35, 
"best_offer_shiping_price": 0, 
"best_shop_id": 2004, 
"best_shop_name": "Stuff", 
"cat_id": 69, 
"grey_dot": true, 
"is_exclusivity": null, 
"is_favorite": false, 
"is_new": false, 
"is_topsales": false, 
"manufacturer_id": 58, 
"name": "my product name", 
"nature_mw": 11, 
"note": "0.0000", 
"offers_count": 11, 
"offers_min_price": 233.21, 
"products_ids": 30671, 
"promo_price_mw": 249.99, 
"status": 1
}

I want to make it tsv with jq, but jq says:

jq: error (at <stdin>:1): object ({"products_...) cannot be tsv-formatted, only array

I can't see why

the full command I'm passing is :

jq  '@tsv' file.json

I tried -c or -r and -R options with no luck. I can't see why this doesn't work Thanks for your help

like image 254
glitchtracker Avatar asked Feb 13 '18 10:02

glitchtracker


2 Answers

If you just want the values (without headers):

[.[]] | @tsv

If you want the headers as well:

(keys_unsorted, [.[]]) | @tsv
like image 176
peak Avatar answered Oct 23 '22 18:10

peak


It would be:

jq -r 'to_entries|map(.value)|@tsv' file.json

to_entries transforms the input into:

[
  {
    "key": "base_price_mw",
    "value": 249.99
  },
  {
    "key": "best_offer_base_price",
    "value": 280.06
  },
  {
    "key": "best_offer_nature",
    "value": 11
  },
  ...
]

... we get only the values from that using map(.value) and pass that to @tsv

like image 23
hek2mgl Avatar answered Oct 23 '22 19:10

hek2mgl