Source

formatJSON /

Filename Size Date modified Message
src
4.5 KB

formatJSON

This jQuery plugin formats a JSON string into HTML. It includes toggling of nested arrays and objects. For a live example view http://jsfiddle.net/jsumners/38uK4/.

How To Use It

The simplest usage of the plugin merely requires an HTML element that contains a JSON string. For example:

<div id="myJSON">
  {"foo":"bar", "items":[1,2,3,4]}
</div>

<script>
  // Format the JSON in the #myJSON element.
  $("#myJSON").formatJSON();
</script>

You could also pass in a JSON string:

<div id="jsonOutput">
</div>

<script>
  $("#jsonOutput").formatJSON({
      jsonString: '{"foo":"bar", "items":[1,2,3,4]}'
    });
</script>

Note: the resulting HTML writes to an unordered list with an id attribute set to "formattedJSON". If you intend to have multiple instances of formatted JSON in the same document then you must specify unique identifiers for each format. This is done by passing in a value for the jsonListId option.

Options

Options for formatJSON are passed in via an object literal in the following format:

{
  classes: {
    array: "string",
    closeBracket: "string",
    closedGroup: "string",
    disclosure: "string",
    keyName: "string",
    keyNameSeparator: "string",
    keyword: "string",
    number: "string",
    object: "string",
    openBracket: "string",
    separator: "string",
    string: "string"
  },
  collapse: boolean,
  expandRoot: boolean,
  jsonListId: "string",
  events: {
    complete: function,
    disclosureClicked: function
  }
}

All properties are optional. The description of each property is as follows:

  • classes: This object specifies the class names that will be used in the resulting markup.
  • array: Applied to unordered lists that represent arrays.
  • closeBracket: Applied to the closing brackets ] and }.
  • closedGroup: Applied to unordered lists when then list items are hidden.
  • disclosure: Applied to elements that are used for toggling the display of a group.
  • keyName: Applied to object property names.
  • keyNameSeparator: Applied to the separator between property names and values.
  • keyword: Applied to the keywords true, false, and null.
  • number: Applied to number values.
  • object: Applied to unordered lists that represent objects.
  • openBracket: Applied to the opening brackets [ and {.
  • separator: Applied the separator between items in arrays and objects.
  • string: Applied to string values.
  • collapse: Indicates if the formatted JSON should be collapsed initially. (default: true)
  • expandRoot: Indicates if the root element of the formatted JSON should be expanded initially. (default: true)
  • jsonListId: Used as the identifier for the generated unordered list that represents the JSON string. (default: "formattedJSON")
  • jsonString: The JSON string that will be formatted. (default: the text of the target element)
  • events: An object of event callback functions.
  • complete: Will be called when formatJSON has finished. The jQuery object representing the initial target element will be passed in as the only parameter. (default: an empty function)
  • disclosureClicked: Will be called when a disclosure element is clicked. This will be called by the plugin if collapse or expandRoot are true.

License

The MIT License (MIT) Copyright (c) <2013> <James Sumners>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.