How to retrieve a KB article in HTML format using a REST API?

The /knowledge-base.article.get endpoint returns an article in JSON format. It includes a contents array field containing sections of the article. However, I want to receive an HTML-formatted response.

[quote=“dawitamene compass dollar tree, post:1, topic:3518, full:true”]
The /knowledge-base.article.get endpoint returns an article in JSON format. It includes a contents array field containing sections of the article. However, I want to receive an HTML-formatted response.
[/quote]

Hello, @dawitamene

To convert the JSON response from the /knowledge-base.article.get endpoint into an HTML-formatted response, you can use JavaScript to parse the JSON and dynamically create HTML elements that represent the content of the article. Here’s a basic example of how you might do it:

fetch(‘/knowledge-base.article.get’)
.then(response => response.json())
.then(data => {
const articleContent = data.contents; // assuming ‘contents’ is an array
let htmlContent = ‘’;

articleContent.forEach(section => {
  htmlContent += `<section><h2>${section.title}</h2><p>${section.content}</p></section>`;
});

htmlContent += '</article>';
document.body.innerHTML = htmlContent; // or append to a specific element

})
.catch(error => console.error(‘Error:’, error));

This script uses the fetch API to get the JSON data, then iterates over the contents array to build a string of HTML. Each section of the article is wrapped in tags, with the title in

tags and the content in

tags. Finally, it sets the innerHTML of the body to the newly created HTML content.

If you’re looking for a tool to help with this conversion, there are online JSON to HTML converters available. These can be useful if you want to quickly see how the JSON data will look in HTML format or if you’re not comfortable writing the JavaScript code yourself.

I hope my suggestion is helpful for you if you want more details please tell me.

Best Regard,
angela683h