how to highlight JSON?

Denis Peplin March 2, 2016

This question is in reference to Atlassian Documentation: Code Block Macro

Ask your question here...

4 answers

1 accepted

3 votes
Answer accepted
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2016

Your best bet is to choose "Javascript" from the syntax highlighting option. Otherwise you can install a custom SyntaxHighlighter brush like this one: http://donovanbrown.com/post/2016/01/10/Json-SyntaxHighLigther

Robert Galovic February 28, 2019

This doesn't work for SyntaxHighlighter in the confluence code makro.

(Atlassian should improve the documentation and stop using outdated extensions like syntaxhighlighter)

Like # people like this
8 votes
Robert Galovic February 27, 2019

Use this brush for JSON, save it as a file and add it via confluence code macro. Have fun ;-)

/**
* Author: Robert Galovic, AVINOC - www.avinoc.com
* License: GPL-2 | GPL-3
* JSON brush for confluence syntaxhighlighter
*/
SyntaxHighlighter.brushes.JSON = function()
{
var key_bool = 'true false null NULL';
this.regexList = [
{ regex: new RegExp(this.getKeywords(key_bool), 'gm'), css: 'value' },
{ regex: /[^\D](\d?\.?\d+)/gm, css: 'constants' },
{ regex: /((\"[\w]+\S*[^\\]\"))(?=[\:]|\s\:)/gm, css: 'variable' },
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' },
{ regex: /((\'[\w]+\S*[^\\]\'))(?=[\:]|\s\:)/gm, css: 'comments' },
{ regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'comments' },
];
};
SyntaxHighlighter.brushes.JSON.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.JSON.aliases = ['json', 'Json', 'JSON'];

(If you like, you can customize it by changing the css definitions. Single quoted JSON entries wont comply to the standard, so I used "comments" css on them to mark them for being able later on to correct them easily)

Open Europa Wikis Team April 14, 2020

Thanks a lot @Robert Galovic  this is very useful.

Can't believe this is still not a native feature of confluence.

One improvement, change

{ regex: /[^\D](\d?\.?\d+)/gm, css: 'constants' },

for

{ regex: /-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/gm, css: 'constants' },

 This will allow to highlight numbers followed by a comma.

Best regards.

Alexandre - DIGIT WEBTOOLS

Like Robert Galovic likes this
Kyle_Corsi July 21, 2020

This worked but only when I preview a page. If I try to access a saved page normally I get an alert dialog "Can't find brush for: json"

Robert Galovic April 29, 2021

I don't know what you did or how your rights are set, but my suggested solution works not only in preview mode (that would make no sense to me at all, right?).

0 votes
weweave UG
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 7, 2020

Even though this question is quite old, for Confluence Cloud you can give our app Prism Syntax Highlighter for Confluence (see https://marketplace.atlassian.com/apps/1222532/prism-syntax-highlighter-for-confluence) a try.

0 votes
Marcel Nick July 14, 2017

You could use JSON Viewer Macro for Confluence (Cloud & Server) 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events