I'm trying to make a .txt list of cities from a dataset of cities with more than 15000 people. The JSON is structured like this:
[
{
"country": "United States",
"geonameid": 5376890,
"name": "Newport Beach",
"subcountry": "California"
},
{
"country": "United States",
"geonameid": 5377100,
"name": "Nipomo",
"subcountry": "California"
},
{
"country": "United States",
"geonameid": 5377199,
"name": "Norco",
"subcountry": "California"
},
{
"country": "United States",
"geonameid": 5377613,
"name": "North Glendale",
"subcountry": "California"
},
{
"country": "United States",
"geonameid": 5377640,
"name": "North Highlands",
"subcountry": "California"
}
]
I want to take this and make it into a newline-delimited list of all the things tagged "name" like this:
Newport Beach
Nipomo
Norco
North Glendale
North Highlands
I tried to do this on the command line using a tool I found called jq, which I though would work by writing something like
cat world_cities.json | jq '.name' > cities_list.txt
but I got an error that said "jq: error (at :0): Cannot index array with string "name"". Of course, I'm sure I'm not fully understanding how jq is supposed to work, and I don't have very much experience parsing JSON, but I'm having trouble finding an answer to my specific problem online. Does anyone know what I can potentially do to achieve what I'm trying to achieve from the command line?
If you want to see the whole dataset I'm trying to parse, you can find it here: https://pkgstore.datahub.io/core/world-cities/world-cities_json/data/5b3dd46ad10990bca47b04b4739a02ba/world-cities_json.json
CodePudding user response:
$ jq -r '.[].name' world_cities.json
Newport Beach
Nipomo
Norco
North Glendale
North Highlands
The data is surrounded by an array. You can access the elements with .[]
: .
is the array itself and []
gives its elements. Then .name
gives you each element's name.
Finally, use -r
to have jq output raw strings without quotes.