jq:文件格式为过滤器(-f)与库(-L)
jq: file format as filter(-f) vs. library(-L)
❯ jq --version
jq-1.6
我正在使用 .jq 文件作为过滤器,如下所示,它有效:
❯ cat jq/script.jq
def fi(v):
v | tostring |
if test("\.") then
"float"
else
"integer"
end;
def estype(v):
if type=="number" then
fi(v)
else
type
end;
def esprop(v):
if type=="object" then
{"properties": v | with_entries(.value |= esprop(.))}
else
{"type": estype(v)}
end;
with_entries(.value |= esprop(.))
❯ cat test.json | jq -f jq/script.jq
...(omit results)
但是当我将它用作库时,它会抛出一个错误:
# comment the last filter, except the definitions of functions
❯ cat jq/script.jq
def fi(v):
v | tostring |
if test("\.") then
"float"
else
"integer"
end;
def estype(v):
if type=="number" then
fi(v)
else
type
end;
def esprop(v):
if type=="object" then
{"properties": v | with_entries(.value |= esprop(.))}
else
{"type": estype(v)}
end;
# with_entries(.value |= esprop(.))
❯ cat test.json | jq -L jq/script.jq 'import script;'
jq: error: syntax error, unexpected IDENT, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
import script;
jq: 1 compile error
这是什么意思,我该如何调试和修复它?
.jq 文件是过滤器还是库有不同的语法(doesn't seems like that)?
1a。这是什么意思?
syntax error, unexpected IDENT, expecting FORMAT or QQSTRING_START
这意味着解析器在需要字符串的位置找到了标识符。 (FORMAT 是像 @csv
或 @text
这样的格式化程序的标记,而 QQSTRING_START 是像 "script"
这样的字符串的标记。实际上,在这里使用格式化程序是没有用的因为它不会让你使用 non-constant 字符串,但解析器不知道。)
1b。如何调试和修复此问题?
可能最容易回顾 manual。它说“导入”的预期形式是
import RelativePathString as NAME;
“包含”的预期形式是
include RelativePathString;
它缺少示例来 100% 清楚地说明这一点,但“RelativePathString”是一个占位符 - 它需要是一个文字字符串。尝试其中之一:
cat test.json | jq -L jq 'include "script"; with_entries(.value |= esprop(.))'
cat test.json | jq -L jq 'import "script" as script; with_entries(.value |= script::esprop(.))'
注意库路径应该是你脚本所在的目录,include和import的区别
2。用作过滤器或库的 .jq 文件是否具有不同的语法?
它们使用相同的语法。问题出在导入语句上,而不是脚本文件上。
❯ jq --version
jq-1.6
我正在使用 .jq 文件作为过滤器,如下所示,它有效:
❯ cat jq/script.jq
def fi(v):
v | tostring |
if test("\.") then
"float"
else
"integer"
end;
def estype(v):
if type=="number" then
fi(v)
else
type
end;
def esprop(v):
if type=="object" then
{"properties": v | with_entries(.value |= esprop(.))}
else
{"type": estype(v)}
end;
with_entries(.value |= esprop(.))
❯ cat test.json | jq -f jq/script.jq
...(omit results)
但是当我将它用作库时,它会抛出一个错误:
# comment the last filter, except the definitions of functions
❯ cat jq/script.jq
def fi(v):
v | tostring |
if test("\.") then
"float"
else
"integer"
end;
def estype(v):
if type=="number" then
fi(v)
else
type
end;
def esprop(v):
if type=="object" then
{"properties": v | with_entries(.value |= esprop(.))}
else
{"type": estype(v)}
end;
# with_entries(.value |= esprop(.))
❯ cat test.json | jq -L jq/script.jq 'import script;'
jq: error: syntax error, unexpected IDENT, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
import script;
jq: 1 compile error
这是什么意思,我该如何调试和修复它?
.jq 文件是过滤器还是库有不同的语法(doesn't seems like that)?
1a。这是什么意思?
syntax error, unexpected IDENT, expecting FORMAT or QQSTRING_START
这意味着解析器在需要字符串的位置找到了标识符。 (FORMAT 是像 @csv
或 @text
这样的格式化程序的标记,而 QQSTRING_START 是像 "script"
这样的字符串的标记。实际上,在这里使用格式化程序是没有用的因为它不会让你使用 non-constant 字符串,但解析器不知道。)
1b。如何调试和修复此问题?
可能最容易回顾 manual。它说“导入”的预期形式是
import RelativePathString as NAME;
“包含”的预期形式是
include RelativePathString;
它缺少示例来 100% 清楚地说明这一点,但“RelativePathString”是一个占位符 - 它需要是一个文字字符串。尝试其中之一:
cat test.json | jq -L jq 'include "script"; with_entries(.value |= esprop(.))'
cat test.json | jq -L jq 'import "script" as script; with_entries(.value |= script::esprop(.))'
注意库路径应该是你脚本所在的目录,include和import的区别
2。用作过滤器或库的 .jq 文件是否具有不同的语法?
它们使用相同的语法。问题出在导入语句上,而不是脚本文件上。