cam*_*258 6 bash json types jq
我正在尝试检查属于特定键的 JSON 值的实际数据类型。
test.json
{
"id": 50,
"name": "Joe"
}
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西:
$ `jq 'typeof("id")' < test.json`
(output)>> string
Run Code Online (Sandbox Code Playgroud)
这可以使用jq吗?
hek*_*mgl 16
使用type:
jq -r '[1.23,"abc",true,[],{},null][]| type' <<< '""'
number
string
boolean
array
object
null
Run Code Online (Sandbox Code Playgroud)
在您的示例中,您可以检查:
jq '.id|type=="number"' file.json
Run Code Online (Sandbox Code Playgroud)
或者在select过滤器中使用它来显示那些不是数字的id,例如:
jq '.[]|select(id|type=="number"|not)' file.json
Run Code Online (Sandbox Code Playgroud)