TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if there is a good way for JSON files to be listed with the listings package.

The only language definition I could come up with, is this:


Now, this highlights the strings used in JSON files, but the important syntactical things in JSON are the curly braces, the square brackets, commas, and colons. I have sadly no Idea how I could make them be highlighted in a different manner. I tried adding { and } as identifier or as keywords, but it didn't work.

I'd really like to make listed JSON files appear nicer, since on a project I need to document I'd really need it. Also, I couldn't find a JSON definition for that on the internet, anywhere else.

On a more general thought: can I highlight all numerals with listings?

share|improve this question
the full json definition is on the page json.org – David Carlisle Nov 17 '12 at 15:04
@DavidCarlisle yes, I know that, but I still don't know how to construct a language definition for listings. Symbols such as { are not accepted as keywords. Or I'm doing something wrong. – polemon Nov 17 '12 at 15:39
ah sorry I misread your comment – David Carlisle Nov 17 '12 at 15:55
I updated my answer; I initially overlooked the numerals issue; now I've added it to my answer. – Gonzalo Medina Nov 17 '12 at 16:55
both single- and double-quoted strings should probably have the [b] type as they are backslash-escaped in JSON (and Javascript). – mlc Apr 8 '14 at 20:01
up vote 32 down vote accepted

Here's one possibility for the colon, comma, braces, square brackets, and numbers, using the literate key:

\usepackage{bera}% optional: just to have a nice mono-spaced font




{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}


enter image description here

share|improve this answer
One issue is that in a number-alphabet combination (such as a web address), the numbers also get highlighted in the same manner as standalone numbers, while alphabets don't get highlighted, which makes it a little bit weird. – JI Xiang Dec 14 '14 at 6:47
Another issue is that string literals don't behave as such; for instance, if I have a { inside a string literal, it's also marked up. My example is { "fmt": "CREATE %{persistence}s TABLE %{identity}D %{if_not_exists}s (%{table_elements:, }s) %{inherits}s %{on_commit}s %{tablespace}s" } – alvherre Jul 17 '15 at 10:24
@XiangJi morestring=[b]" after the last literate, i.e. after {]}{{{\color{delim}{]}}}}{1}, treats everything within double quotes as a String, overwriting the literate-behaviour for numbers. You might have to also define a stringstyle in order to get a consistent layout for your listing. – PattaFeuFeu Jan 4 at 12:58
wow this is really nice, thanks!! – Tommy Jun 27 at 14:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.