你可以使用 jq 来调用外部命令来转换 JSON。

huangapple go评论47阅读模式
英文:

How can I invoke external command to transform json using jq?

问题

以下是你要翻译的内容:

I have the following json input:

{
  "url": "https://www.example.com", 
  "html": "<html>...</html>"
}

How can I use jq to extract all JavaScript <script> tags from html using pup?

For an example, I can extract all scripts I want from a single html using a pipe call:

cat example.json | jq -r .html | pup 'script[type="text/javascript"] text{}'

I would like to put all of these extracted scripts in a new resulting json:

{
  "url": "https://www.example.com",
  "scripts": [
    "<script>...",
    "<script>..."
  ]
}

If I try using:

jq -c '{url: .url, scripts: [.html | pup "script[type=text/javascript] text{}"]}'

it will not work because pup is an external command and not part of jq.

How can I achieve this?

英文:

I have the following json input:

{
  &quot;url&quot;: &quot;https://www.example.com&quot;, 
  &quot;html&quot;: &quot;&lt;html&gt;...&lt;/html&gt;&quot;
}

How can I use jq to extract all JavaScript &lt;script&gt; tags from html using pup?

For an example, I can extract all scripts I want from a single html using a pipe call:

cat example.json | jq -r .html | pup &#39;script[type=&quot;text/javascript&quot;] text{}&#39;

I would like to put all of these extracted scripts in a new resulting json:

{
  &quot;url&quot;: &quot;https://www.example.com&quot;,
  &quot;scripts&quot;: [
    &quot;&lt;script&gt;...&quot;,
    &quot;&lt;script&gt;...&quot;
  ]
}

If I try using:

jq -c &#39;{url: .url, scripts: [.html | pup &quot;script[type=text/javascript] text{}&quot;]}&#39;

it will not work because pup is an external command and not part of jq.

How can I achieve this?

答案1

得分: 2

这是您要翻译的部分:

It's not possible directly from jq (jq cannot call external programs from within a jq program). But if your input only contains a single object with those two properties, the following should work in POSIX shells:

{
  jq '{url}' input.json;
  jq -r '.html' input.json | pup ... | jq -Rs '{scripts: .}';
} | jq -s 'add'

It's also possible to invoke jq with --arg – which still invokes jq 2 times and reads your input twice:

jq --arg scripts "$(jq -r '.html' input.json | pup ... )" \
'{url, $scripts}' input.json

Sample output:

{
  "url": "http://example.com",
  "scripts": "...."
}
英文:

It's not possible directly from jq (jq cannot call external programs from within a jq program). But if your input only contains a single object with those two properties, the following should work in POSIX shells:

{
  jq &#39;{url}&#39; input.json;
  jq -r &#39;.html&#39; input.json | pup ... | jq -Rs &#39;{scripts: .}&#39;;
} | jq -s &#39;add&#39;

It's also possible to invoke jq with --arg – which still invokes jq 2 times and reads your input twice:

jq --arg scripts &quot;$(jq -r &#39;.html&#39; input.json | pup ... )&quot; \
&#39;{url, $scripts}&#39; input.json

Sample output:

{
  &quot;url&quot;: &quot;http://example.com&quot;,
  &quot;scripts&quot;: &quot;....&quot;
}

答案2

得分: 2

请使用 json{} 代替 text{} 以启用 jq 的后处理。例如:

jsonfile='example.json'
jq '.scripts = (input | map(.text // empty))' "$jsonfile" <(
  jq -r '.html' "$jsonfile" | pup 'script[type=text/javascript] json{}'
)
英文:

Use json{} instead of text{} to enable post-processing with jq. For example:

jsonfile=&#39;example.json&#39;
jq &#39;.scripts = (input | map(.text // empty))&#39; &quot;$jsonfile&quot; &lt;(
  jq -r &#39;.html&#39; &quot;$jsonfile&quot; | pup &#39;script[type=text/javascript] json{}&#39;
)

huangapple
  • 本文由 发表于 2023年3月7日 19:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定