使用 JQ 将 HTML 文件插入 JSON 文件值
Insert an HTML file into a JSON file value using JQ
目标
我的目标是使用 Jive API 和 cURL 命令将 HTML 内容推送到 Jive 内容管理平台。
现状
我可以成功完成此操作,但我的过程是手动的。我想通过编写脚本将 HTML 插入 JSON 文件来进一步自动化 Jive 需要通过 API.[=21= post HTML 内容]
JSON 文件示例
{
"entityType": "document",
"content": {
"type": "text/html",
"text": "ENCODED HTML FILE CONTENT AS A STRING"
},
"type": "document",
"subject": "Document Title",
"visibility": "place",
"parent": "https://<URL>/api/core/v3/places/1579"
}
HTML 文件示例
<html lang="en">
<body>
<h1 id="example">Sample file</h1>
<p>Sample text</p>
<table>
<thead>
<tr class="header">
etc.
cURL 示例
curl -X PUT -u username:password -H "Content-Type: application/json" -H "Accept: application/json" -H "Cache-Control: no-cache" -d @file.json "https://<URL>/api/core/v3/contents/12204"
Jive API does not turn a standalone .html file sent over the API into HTML content 而是将其视为任何上传的二进制文件,必须由用户下载。
尝试解决
A 询问有关以相同方式插入 .txt 文件的问题。接受的答案建议我尝试:
jq --slurpfile text file.html '.text=$text' file.json >newfile.json
我试过用引号将文件括起来(在编码和字符串化之后),但也失败了。
为什么选择 cURL 和 JQ
我正在寻找 JQ + cURL 解决方案的原因是 JQ 和 cURL 都可以 运行 带有 Bash 脚本,这是我所知道的唯一编程。
奖金问题
为了使 HTML 文件成为编码字符串,我 运行 sed 's/&/\&/g; s/"/\\"/g; s/'"'"'/\\'"'"'/g'
并删除所有硬 returns。我相信 JQ 的原始输入和 slurp 选项可以用 jq -Rs '{ text: . }' file.html
之类的东西解决这个问题(根据这个 , and JQ offers an @json 转义 HTML 的方法,但经过多次网络搜索和尝试后,我都失败了。 运行 一个 JQ 命令很好,encoded/stringified HTML 和 都将 HTML 插入到 .json 文件.
如果这完全是错误的方法...
这个问题中描述的解决方案是我尝试解决我的问题,我希望从更广阔的角度来看我的基本假设和方法是否不如其他可能的方法。
谢谢。
如果您的 HTML 文件足够短,它可以放入命令行:
jq --arg text "$(<file.html)" '.content.text=$text' file.json >newfile.json
或者,运行 jq
的额外副本,但可以处理任意大小的文件:
jq --slurpfile texts <(jq -Rs file.html) '.content.text=$texts[0]' file.json >newfile.json
关于 "Bonus Question" 关于编码 HTML 文件的问题,您可以简单地使用过滤器 @html
,例如
.content.text=($text | @html)
来自联机手册:
@html:
Applies HTML/XML escaping, by mapping the characters <>&'" to their
entity equivalents <, >, &, ', ".
目标
我的目标是使用 Jive API 和 cURL 命令将 HTML 内容推送到 Jive 内容管理平台。
现状
我可以成功完成此操作,但我的过程是手动的。我想通过编写脚本将 HTML 插入 JSON 文件来进一步自动化 Jive 需要通过 API.[=21= post HTML 内容]
JSON 文件示例
{
"entityType": "document",
"content": {
"type": "text/html",
"text": "ENCODED HTML FILE CONTENT AS A STRING"
},
"type": "document",
"subject": "Document Title",
"visibility": "place",
"parent": "https://<URL>/api/core/v3/places/1579"
}
HTML 文件示例
<html lang="en">
<body>
<h1 id="example">Sample file</h1>
<p>Sample text</p>
<table>
<thead>
<tr class="header">
etc.
cURL 示例
curl -X PUT -u username:password -H "Content-Type: application/json" -H "Accept: application/json" -H "Cache-Control: no-cache" -d @file.json "https://<URL>/api/core/v3/contents/12204"
Jive API does not turn a standalone .html file sent over the API into HTML content 而是将其视为任何上传的二进制文件,必须由用户下载。
尝试解决
A
jq --slurpfile text file.html '.text=$text' file.json >newfile.json
我试过用引号将文件括起来(在编码和字符串化之后),但也失败了。
为什么选择 cURL 和 JQ
我正在寻找 JQ + cURL 解决方案的原因是 JQ 和 cURL 都可以 运行 带有 Bash 脚本,这是我所知道的唯一编程。
奖金问题
为了使 HTML 文件成为编码字符串,我 运行 sed 's/&/\&/g; s/"/\\"/g; s/'"'"'/\\'"'"'/g'
并删除所有硬 returns。我相信 JQ 的原始输入和 slurp 选项可以用 jq -Rs '{ text: . }' file.html
之类的东西解决这个问题(根据这个
如果这完全是错误的方法...
这个问题中描述的解决方案是我尝试解决我的问题,我希望从更广阔的角度来看我的基本假设和方法是否不如其他可能的方法。
谢谢。
如果您的 HTML 文件足够短,它可以放入命令行:
jq --arg text "$(<file.html)" '.content.text=$text' file.json >newfile.json
或者,运行 jq
的额外副本,但可以处理任意大小的文件:
jq --slurpfile texts <(jq -Rs file.html) '.content.text=$texts[0]' file.json >newfile.json
关于 "Bonus Question" 关于编码 HTML 文件的问题,您可以简单地使用过滤器 @html
,例如
.content.text=($text | @html)
来自联机手册:
@html:
Applies HTML/XML escaping, by mapping the characters <>&'" to their entity equivalents <, >, &, ', ".