使用 jq 访问嵌套数组
Using jq to access nested arrays
鉴于此数据(直接来自 census.gov api):
[["P0010001","NAME","state"],
["4779736","Alabama","01"],
["710231","Alaska","02"],
["6392017","Arizona","04"],
["2915918","Arkansas","05"],
["37253956","California","06"],
["5029196","Colorado","08"],
["3574097","Connecticut","09"],
["897934","Delaware","10"],
["601723","District of Columbia","11"],
["18801310","Florida","12"],
["9687653","Georgia","13"],
["1360301","Hawaii","15"],
["1567582","Idaho","16"],
["12830632","Illinois","17"],
["6483802","Indiana","18"],
["3046355","Iowa","19"],
["2853118","Kansas","20"],
["4339367","Kentucky","21"],
["4533372","Louisiana","22"],
["1328361","Maine","23"],
["5773552","Maryland","24"],
["6547629","Massachusetts","25"],
["9883640","Michigan","26"],
["5303925","Minnesota","27"],
["2967297","Mississippi","28"],
["5988927","Missouri","29"],
["989415","Montana","30"],
["1826341","Nebraska","31"],
["2700551","Nevada","32"],
["1316470","New Hampshire","33"],
["8791894","New Jersey","34"],
["2059179","New Mexico","35"],
["19378102","New York","36"],
["9535483","North Carolina","37"],
["672591","North Dakota","38"],
["11536504","Ohio","39"],
["3751351","Oklahoma","40"],
["3831074","Oregon","41"],
["12702379","Pennsylvania","42"],
["1052567","Rhode Island","44"],
["4625364","South Carolina","45"],
["814180","South Dakota","46"],
["6346105","Tennessee","47"],
["25145561","Texas","48"],
["2763885","Utah","49"],
["625741","Vermont","50"],
["8001024","Virginia","51"],
["6724540","Washington","53"],
["1852994","West Virginia","54"],
["5686986","Wisconsin","55"],
["563626","Wyoming","56"],
["3725789","Puerto Rico","72"]]
我想使用 jq 生成如下所示的文件:
{
"population": "4779736",
"state_name": "Alabama",
"state_code": "01"
}
{
"population": "710231",
"state_name": "Alaska",
"state_code": "02"
}
{
"population": "6392017",
"state_name": "Arizona",
"state_code": "04"
}
// etc...
我试过:
.[1:999999999999] | {population:.[][0], state_name:.[][1], state_code:.[][2]}
[1:999999999999]
就是去掉第一个数组。它可以工作,但它会生成数千个 JS 对象,而不是仅仅 53 个。它似乎一遍又一遍地重复映射。这是为什么?
jq 可以在 https://jqplay.org/
进行测试
正确的方法是使用map()
:
.[1:999999999999] | map({population:.[0], state_name:.[1], state_code:.[2]})
建议的答案可以在两个方面进行改进:
(1) 而不是 .[1:999999999999] hack,你可以简单地写 .[1:].
(2) 声明的目标是生成对象序列(即流),而不是对象数组。这可以通过以下方式完成:
.[1:][] | {population:.[0], state_name:.[1], state_code:.[2]}
鉴于此数据(直接来自 census.gov api):
[["P0010001","NAME","state"],
["4779736","Alabama","01"],
["710231","Alaska","02"],
["6392017","Arizona","04"],
["2915918","Arkansas","05"],
["37253956","California","06"],
["5029196","Colorado","08"],
["3574097","Connecticut","09"],
["897934","Delaware","10"],
["601723","District of Columbia","11"],
["18801310","Florida","12"],
["9687653","Georgia","13"],
["1360301","Hawaii","15"],
["1567582","Idaho","16"],
["12830632","Illinois","17"],
["6483802","Indiana","18"],
["3046355","Iowa","19"],
["2853118","Kansas","20"],
["4339367","Kentucky","21"],
["4533372","Louisiana","22"],
["1328361","Maine","23"],
["5773552","Maryland","24"],
["6547629","Massachusetts","25"],
["9883640","Michigan","26"],
["5303925","Minnesota","27"],
["2967297","Mississippi","28"],
["5988927","Missouri","29"],
["989415","Montana","30"],
["1826341","Nebraska","31"],
["2700551","Nevada","32"],
["1316470","New Hampshire","33"],
["8791894","New Jersey","34"],
["2059179","New Mexico","35"],
["19378102","New York","36"],
["9535483","North Carolina","37"],
["672591","North Dakota","38"],
["11536504","Ohio","39"],
["3751351","Oklahoma","40"],
["3831074","Oregon","41"],
["12702379","Pennsylvania","42"],
["1052567","Rhode Island","44"],
["4625364","South Carolina","45"],
["814180","South Dakota","46"],
["6346105","Tennessee","47"],
["25145561","Texas","48"],
["2763885","Utah","49"],
["625741","Vermont","50"],
["8001024","Virginia","51"],
["6724540","Washington","53"],
["1852994","West Virginia","54"],
["5686986","Wisconsin","55"],
["563626","Wyoming","56"],
["3725789","Puerto Rico","72"]]
我想使用 jq 生成如下所示的文件:
{
"population": "4779736",
"state_name": "Alabama",
"state_code": "01"
}
{
"population": "710231",
"state_name": "Alaska",
"state_code": "02"
}
{
"population": "6392017",
"state_name": "Arizona",
"state_code": "04"
}
// etc...
我试过:
.[1:999999999999] | {population:.[][0], state_name:.[][1], state_code:.[][2]}
[1:999999999999]
就是去掉第一个数组。它可以工作,但它会生成数千个 JS 对象,而不是仅仅 53 个。它似乎一遍又一遍地重复映射。这是为什么?
jq 可以在 https://jqplay.org/
进行测试正确的方法是使用map()
:
.[1:999999999999] | map({population:.[0], state_name:.[1], state_code:.[2]})
建议的答案可以在两个方面进行改进:
(1) 而不是 .[1:999999999999] hack,你可以简单地写 .[1:].
(2) 声明的目标是生成对象序列(即流),而不是对象数组。这可以通过以下方式完成:
.[1:][] | {population:.[0], state_name:.[1], state_code:.[2]}