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

Pulling specific data out of JSON via jq to CSV using Bash

$
0
0

I'm working on writing a script to extract specific data from a JSON array and convert it into a CSV. I'm retrieving the data from an API with data that looks like this:

{"data": [        {"id": "345","market": "storage","name": "StorageHub","description": null,"address_line1": "100 Main St","address_city": "Cleveland","address_state": "OH","address_country": "USA","address_postal_code": "91801","address_latitude": 42.433243,"address_longitude": -82.3941872        }    ]}

I'm looking to only extract the id, market, name, and city into a new CSV named {account_id}_exercise1.csv, with the account id being from a variable. The code below successfully returns a csv but with all the data from the above dataset, not just the 4 date pieces I want.

account_id=398GET "accounts/${account_id}/properties" csv > "files/${account_id}_exercise1.csv" | while read -r row; do    asset_id=$(echo "$row" | jq -r '.id')    asset_name=$(echo "$row" | jq -r '.name')    market_detail=$(echo "$row" | jq -r '.market')    unit_count=$(echo "$row" | jq -r '.unit_count')    echo "Property: ${account_id} completed.."  done

Viewing all articles
Browse latest Browse all 524

Trending Articles



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