hcdocs
hcdocs

Reputation: 1299

Insert an HTML file into a JSON file value using JQ

Goal

My goal is to push HTML content to the Jive content-management platform using the Jive API and a cURL command.

Current status

I can do this successfully but my process is manual. I want to further automate by scripting the insertion of the HTML into the JSON file Jive requires to post HTML content through the API.

JSON file example

{
    "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 file example

<html lang="en">
<body>
<h1 id="example">Sample file</h1>
    <p>Sample text</p>
<table>
    <thead>
        <tr class="header">

etc.

cURL example

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"

The Jive API does not turn a standalone .html file sent over the API into HTML content but rather treats it as any uploaded binary file, which must be downloaded by the user.

Attempts to solve

A similar question in Stack Overflow asks about inserting a .txt file in the same manner. The accepted answer suggests that I try:

jq --slurpfile text file.html '.text=$text' file.json >newfile.json

I have tried surrounding the file with quotes (after it is encoded and stringified), which also failed.

Why cURL and JQ

The reason I am looking for a JQ + cURL solution is that both JQ and cURL can be run with a Bash script, which is the only programming I know.

Bonus question

To make the HTML file an encoded string, I run sed 's/&/\&amp;/g; s/"/\\\"/g; s/'"'"'/\\\'"'"'/g' and remove all the hard returns. I believe JQ's raw input and slurp options can solve this with something like jq -Rs '{ text: . }' file.html (according to this Stack overflow question, and JQ offers an @json method that escapes HTML, but after many web searches and attempts I have failed at both. It would be nice to run one JQ command that both encoded/stringified the HTML and inserted the HTML into the .json file.

If this is the wrong approach entirely...

The solutions described in this question are my attempt to solve my problem and I invite a broader perspective as to whether my basic assumptions and approach are inferior to other potential approaches.

Thank you.

Upvotes: 1

Views: 2646

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295619

If your HTML file is short enough it can fit into a command line:

jq --arg text "$(<file.html)" '.content.text=$text' file.json >newfile.json

Alternately, running an extra copy of jq, but working with files of any size:

jq --slurpfile texts <(jq -Rs file.html) '.content.text=$texts[0]' file.json >newfile.json

Upvotes: 4

peak
peak

Reputation: 116860

Regarding the "Bonus Question" about encoding the HTML file, you could simply use the filter @html, e.g.

.content.text=($text | @html)

From the online manual:

@html:

Applies HTML/XML escaping, by mapping the characters <>&'" to their entity equivalents <, >, &, ', ".

Upvotes: 4

Related Questions