英文:
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 script
s 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:
{
"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 script
s 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?
答案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 '{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": "...."
}
答案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='example.json'
jq '.scripts = (input | map(.text // empty))' "$jsonfile" <(
jq -r '.html' "$jsonfile" | pup 'script[type=text/javascript] json{}'
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论