Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 524

How can i use jq to format json to csv?

$
0
0

I am trying to format this json data to CSV format, but I get this error: error (at <stdin>:0): Cannot index array with string "time". Here is a short version of my json data:

{"latitude": 52.52,"longitude": 13.419998,"generationtime_ms": 0.23293495178222656,"utc_offset_seconds": 7200,"timezone": "Europe/Berlin","timezone_abbreviation": "CEST","elevation": 38.0,"daily_units": {"time": "iso8601","weather_code": "wmo code","temperature_2m_max": "°F","temperature_2m_min": "°F","precipitation_probability_max": "%"  },"daily": {"time": ["2024-10-13","2024-10-14"    ],"weather_code": [      80,      61    ],"temperature_2m_max": [      52.7,      53.1    ],"temperature_2m_min": [      48.6,      43.7    ],"precipitation_probability_max": [      100,      10    ]  }}

I want it to be formatted like this:

time,weather_code,temperature_2m_max,temperature_2m_min,precipitation_probability_max2024-10-13,80,52.7,48.6,1002024-10-14,61,53.1,43.7,10

This is the command I wrote, but I get the error jq: error (at <stdin>:0): Cannot index array with string "time". This also happens with each field if i delete all the previous ones.

curl -s "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_probability_max&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=auto&forecast_days=14" | jq -r '  ["date", "weather_code", "temperature_2m_max", "temperature_2m_min", "precipitation_probability_max"  ],  .daily |  [    .time[],    .weather_code[],    .temperature_2m_max[],    .temperature_2m_min[],    .precipitation_probability_max[]  ]  | @csv'

I tried to change the "time" field to a Unix timestamp, but I am still not sure how to get it to work, because it throws the same error.


Viewing all articles
Browse latest Browse all 524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>