Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Arrays 如何在shell脚本中获取JSON中向量的大小?_Arrays_Json_Bash_Count_Jq - Fatal编程技术网

Arrays 如何在shell脚本中获取JSON中向量的大小?

Arrays 如何在shell脚本中获取JSON中向量的大小?,arrays,json,bash,count,jq,Arrays,Json,Bash,Count,Jq,我想在shell脚本中获取JSON中向量的大小,但我只获取每个向量字符串的大小,例如: #!/bin/bash j='{"Names": { "names": ["marry", "jason", "frank","kelly"] }}' echo "$j" | jq -r ".Names | .names[] | length" 输出为: 5 4 5 4 但我想得到4,这是向量中的项数 echo "$j" | jq '.Names | .names | length' 输出: 4 4

我想在shell脚本中获取JSON中向量的大小,但我只获取每个向量字符串的大小,例如:

#!/bin/bash

j='{"Names": { "names": ["marry", "jason", "frank","kelly"] }}'

echo "$j" | jq -r ".Names | .names[] | length"
输出为:

5
4
5
4
但我想得到4,这是向量中的项数

echo "$j" | jq '.Names | .names | length'
输出:

4 4.
jq
代表什么?@EPo,注意问题下标签列表中的“jq”?点击它。你的输出与你的输入不匹配:你如何得到字符串“jason”和“kelly”的长度“4”?