Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json jq-使用连字符分析字段-无效的数字文字_Json_Shell_Jq - Fatal编程技术网

Json jq-使用连字符分析字段-无效的数字文字

Json jq-使用连字符分析字段-无效的数字文字,json,shell,jq,Json,Shell,Jq,我试图使用jq和一些嵌套for循环从API中提取产品类别列表。我必须先提取类别ID,然后才能提取产品详细信息。一些类别ID有hypens,jq似乎把它们当作数学而不是字符串,我尝试了各种引用方式,但仍然遇到了这个错误。在Powershell中,我可以很好地提取列表,但我真的需要它在bash中工作 以下是预期的列表: aprons backpacks beanies bracelet coaster cutting-board dress-shirts duffel-bags earring fu

我试图使用jq和一些嵌套for循环从API中提取产品类别列表。我必须先提取类别ID,然后才能提取产品详细信息。一些类别ID有hypens,jq似乎把它们当作数学而不是字符串,我尝试了各种引用方式,但仍然遇到了这个错误。在Powershell中,我可以很好地提取列表,但我真的需要它在bash中工作

以下是预期的列表:

aprons
backpacks
beanies
bracelet
coaster
cutting-board
dress-shirts
duffel-bags
earring
full-brim-hats
generic-dropoff
hats
etc...
并尝试在Bash中重新创建相同的脚本,以下是输出:

aprons
backpacks
beanies
bracelet
coaster
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 7
parse error: Invalid numeric literal at line 1, column 5
earring
parse error: Invalid numeric literal at line 2, column 0
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 8
hats
etc...
您可以看到,所有包含连字符的值都会出现此错误。以下是我当前的脚本:

#!/bin/bash

CATEGORIES=$(curl -s https://api.scalablepress.com/v2/categories)
IFS=$' \t\n'


for CATEGORY in $(echo $CATEGORIES | jq -rc '.[]')
do
CATEGORY_IDS=$(echo $CATEGORY | jq -rc '."categoryId"')
for CATEGORY_ID in $(echo $CATEGORY_IDS)
do
echo $CATEGORY_ID
PRODUCT_IDS=$(curl -s https://api.scalablepress.com/v2/categories/$CATEGORY_ID | jq -rc '.products[].id')
#for PRODUCT_ID in $(echo $PRODUCT_IDS)
#do
#echo $PRODUCT_ID
#done
done
done

这是一个公开可用的API,因此您应该能够复制此脚本并生成相同的结果。我看到的所有指南都说,如果要解析的字段包含连字符,就在该字段周围加上双引号,但我没有运气这样做。

您可以通过应用选择器:
curl-s为数组中的每个对象选择键
categoryId
https://api.scalablepress.com/v2/categories |jq'地图(.categoryId)“

这将为您提供一个JSON数组,其中只包含您感兴趣的值。然后,您可以使用防粘过滤器
[]
将数组转换为单个结果<然后,code>jq可以使用
-r
开关输出原始字符串

将一切结合在一起,您可以通过一行代码实现您想要的:

curl-shttps://api.scalablepress.com/v2/categories |jq-r'映射(.categoryId)|.[]'


更妙的是,您可以先进行防滑,然后选择要查找的关键点:
curl-shttps://api.scalablepress.com/v2/categories |jq-r'.[]|.categoryId'

通过应用选择器:
curl-s,可以为数组中的每个对象选择键
categoryId
https://api.scalablepress.com/v2/categories |jq'地图(.categoryId)

这将为您提供一个JSON数组,其中只包含您感兴趣的值。然后,您可以使用防粘过滤器
[]
将数组转换为单个结果<然后,code>jq可以使用
-r
开关输出原始字符串

将一切结合在一起,您可以通过一行代码实现您想要的:

curl-shttps://api.scalablepress.com/v2/categories |jq-r'映射(.categoryId)|.[]'


更妙的是,您可以先进行防滑,然后选择要查找的关键点:
curl-shttps://api.scalablepress.com/v2/categories |jq-r'.[]..categoryId'

您可以立即循环类别ID,而无需执行所有破坏json的“回音”。这两个循环可以重写为:

#!/bin/bash

CATURL="https://api.scalablepress.com/v2/categories"

curl -s "$CATURL" | jq -rc '.[] | .categoryId' | while read catid; do
        echo "$catid"
        curl -s "$CATURL/$catid" | jq -rc '.products[].id'
done
这将打印类别id,然后是所有产品id,这些产品id来自您的代码,看起来像是您的最终结果:

$ ./pullcat.sh
aprons
port-authority-port-authority-â-medium-length-apron-with-pouch-pockets
port-authority-port-authority-â-full-length-apron-with-pockets
port-authority-easy-care-reversible-waist-apron-with-stain-release
port-authority-easy-care-waist-apron-with-stain-release
backpacks
port-authority-â-wheeled-backpack
nike-performance-backpack
port-authority-â-value-backpack
port-authority-â-basic-backpack
port-authority-â-cyber-backpack
port-authority-â-commuter-backpack
port-authority-â-contrast-honeycomb-backpack
port-authority-â-camo-xtreme-backpack
port-authority-â-xtreme-backpack
port-authority-â-xcapeâ-computer-backpack
port-authority-â-nailhead-backpack
nike-elite-backpack
port-authority-â-urban-backpack
eddie-bauer-eddie-bauer-â-ripstop-backpack
the-north-face-aurora-ii-backpack
the-north-face-fall-line-backpack
the-north-face-groundwork-backpack
the-north-face-connector-backpack
beanies
rabbit-skins-infant-baby-rib-cap
yupoong-adult-cuffed-knit-cap
ultra-club-adult-knit-beanie-with-cuff
ultra-club-adult-knit-beanie
ultra-club-adult-two-tone-knit-beanie
ultra-club-adult-knit-beanie-with-lid
ultra-club-adult-waffle-beanie
ultra-club-adult-knit-pom-pom-beanie-with-cuff
bayside-beanie
...
如果只需要类别ID,当然可以在循环时“删除”:

#!/bin/bash

CATURL="https://api.scalablepress.com/v2/categories"

curl -s "$CATURL" | jq -rc '.[] | .categoryId'


您可以立即循环类别ID,而无需执行所有破坏json的“回音”。这两个循环可以重写为:

#!/bin/bash

CATURL="https://api.scalablepress.com/v2/categories"

curl -s "$CATURL" | jq -rc '.[] | .categoryId' | while read catid; do
        echo "$catid"
        curl -s "$CATURL/$catid" | jq -rc '.products[].id'
done
这将打印类别id,然后是所有产品id,这些产品id来自您的代码,看起来像是您的最终结果:

$ ./pullcat.sh
aprons
port-authority-port-authority-â-medium-length-apron-with-pouch-pockets
port-authority-port-authority-â-full-length-apron-with-pockets
port-authority-easy-care-reversible-waist-apron-with-stain-release
port-authority-easy-care-waist-apron-with-stain-release
backpacks
port-authority-â-wheeled-backpack
nike-performance-backpack
port-authority-â-value-backpack
port-authority-â-basic-backpack
port-authority-â-cyber-backpack
port-authority-â-commuter-backpack
port-authority-â-contrast-honeycomb-backpack
port-authority-â-camo-xtreme-backpack
port-authority-â-xtreme-backpack
port-authority-â-xcapeâ-computer-backpack
port-authority-â-nailhead-backpack
nike-elite-backpack
port-authority-â-urban-backpack
eddie-bauer-eddie-bauer-â-ripstop-backpack
the-north-face-aurora-ii-backpack
the-north-face-fall-line-backpack
the-north-face-groundwork-backpack
the-north-face-connector-backpack
beanies
rabbit-skins-infant-baby-rib-cap
yupoong-adult-cuffed-knit-cap
ultra-club-adult-knit-beanie-with-cuff
ultra-club-adult-knit-beanie
ultra-club-adult-two-tone-knit-beanie
ultra-club-adult-knit-beanie-with-lid
ultra-club-adult-waffle-beanie
ultra-club-adult-knit-pom-pom-beanie-with-cuff
bayside-beanie
...
如果只需要类别ID,当然可以在循环时“删除”:

#!/bin/bash

CATURL="https://api.scalablepress.com/v2/categories"

curl -s "$CATURL" | jq -rc '.[] | .categoryId'

标记答案,因为它1)解决了我的问题,2)简化了循环。非常感谢。标记答案,因为它1)解决了我的问题,2)简化了循环。非常感谢。