Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structured text in JSON

Tags:

json

text

I have been looking for a way to capture structured text (sections, paragraphs, emphasis, lists, etc.) in JSON, but I haven't found anything yet. Any suggestions? (Markdown crossed my mind, but there might be something better out there.)

like image 499
Wilfred Springer Avatar asked Nov 15 '10 08:11

Wilfred Springer


2 Answers

How about something like this:

[ { "heading": "Foobar Example" },
  { "paragraph":
    [
      "This is normal text, followed by... ",
      { "bold": "some bold text" },
      "etc."
    ]
  }
]

That is:

  • use a string for plain text without formatting or other mark-up;

  • use an array whenever you want to indicate an ordered sequence of certain text elements;

  • use an object where the key indicates the mark-up and the value the text element to which the formatting is applied.

like image 150
stakx - no longer contributing Avatar answered Sep 30 '22 11:09

stakx - no longer contributing


HTML is a well-established way to describe structured text, in a plain-text format(!). Markdown (as you mentioned) would work as well.

My view is that your best bet is probably going to be using some sort of plain-text markup such as those choices, and place your text in a single JSON string variable. Depending on your application, it may make sense to have an array of sections, containing an array of paragraphs, containing an array of normal/bold/list sections etc. However, in the general case I think good old-fashioned blocks are markup will ironically be cleaner and more scalable, due to the ease of passing them around, and the well-developed libraries for full-blown parsing if/when required.

like image 37
Andrzej Doyle Avatar answered Sep 30 '22 11:09

Andrzej Doyle