Git Product home page Git Product logo

jquery.json-viewer's Introduction

jQuery json-viewer

npm npm npm workflow

json-viewer is a jQuery plugin for easily displaying JSON objects by transforming them into HTML.

Features:

  • Syntax highlighting
  • Collapse and expand child nodes on click
  • Clickable links
  • Easily readable and minimal DOM structure
  • Optional support for libraries supporting big numbers

Check out the demo page!

Install

With npm:

npm install jquery.json-viewer

Make sure jQuery is already included. Then import jquery.json-viewer.js and jquery.json-viewer.css in your HTML document:

<head>
  <script src="node_modules/jquery.json-viewer/json-viewer/jquery.json-viewer.js"></script>
  <link href="node_modules/jquery.json-viewer/json-viewer/jquery.json-viewer.css" type="text/css" rel="stylesheet">
</head>

You can also simply copy json-viewer/jquery.json-viewer.js and json-viewer/jquery.json-viewer.css files from this git repository into your project.

Usage

Call the jsonViewer() method on target element and pass your JSON data in argument:

<pre id="json-renderer"></pre>
var data = {
  "foobar": "foobaz"
};
$('#json-renderer').jsonViewer(data);

Options

The jsonViewer method accepts an optional config object as a second argument. The supported options are:

  • collapsed (boolean, default: false): all nodes are collapsed at html generation.
  • rootCollapsable (boolean, default: true): allow root element to be collasped.
  • withQuotes (boolean, default: false): all JSON keys are surrounded with double quotation marks ({"foobar": 1} instead of {foobar: 1}).
  • withLinks (boolean, default: true): all values that are valid links will be clickable, if false they will only be strings.
  • bigNumbers (boolean, default: false): support different libraries for big numbers, if true display the real number only, false shows object containing big number with all fields instead of number only.

Example:

$('#json-renderer').jsonViewer(data, {collapsed: true, withQuotes: true, withLinks: false});

Big number support

Enabling bigNumbers the json object visible will show the number stored inside the object only and does not display all fields.

Example Object using Decimal.js - other libraries are similar:

var Decimal = require('decimal.js');
var data = { "x": new Decimal(123) };

$('#json-renderer').jsonViewer(data, {bigNumbers: false});

// {
//   "x": {
//        "constructor": ,
//        "s": 1,
//        "e": 2,
//        "d": [
//          123
//        ]
//   }
// }

$('#json-renderer').jsonViewer(data, {bigNumbers: true});

// {
//   "x": 123
// }

The following libraries are supported:

About

jquery.json-viewer's People

Contributors

abodelot avatar daszh avatar idleft avatar jason-lynch avatar lillypad avatar punxnotdead avatar sseide avatar vasco-jofra avatar weidongdong411 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.json-viewer's Issues

nesting level

can I somehow control the level of nesting? How is it easiest for me to implement?
nesting (int 0-5, default 3)

Ability to parse a string field as json

My json has a field, it may contain a regular message or another json as string. It would be nice to have a feature that lets me to click that field, perform action as Decode as json, and string field becomes a property, and its contents are displayed a json too as if it was just another object property.

collapsed ?

Hi,
Searching(ctrl+f) on the page does not work when using collapsed = true;
image

after manuel open:
image

Provide option for showing the spaces after keys

Hi,
when I have the following JSON content:

"x" : 10

this viewer shows it as:

"x": 10

and removes the space character after the key.
Can you provide an option to keep the space?
It would be helpful when copying the JSON to keep it more similar to the original.

Provide some kind of render callback API

Hi,
I am using a heavily modified version of this since I render stuff different depending on the context of the JSON.
Somtimes I want to change values to customized URLs.
Sometimes I want to fold stuff by default.

Maybe support a generic render API like:

function customRenderer(json, options) {
return {
    rendered: json2html(json, options),
    collapsed: json instanceof Array,
};
}

where o is the currently traversed json key and value.
By default you do not have to register this renderer but you can do it by passing it to the options or something.

This would reduce the number of forked repositories since users can heavily modify this viewer without changing the actualy code.

Not working with list

Not working with below json:

page becomes irresponsive

{
   "randomUUID": [
      "91563848-3db7-47af-8dbb-825721e8907b",
      "826b23ce-2669-4122-981f-3e2e4429159d",
      "e32111a0-6a87-49ab-b58f-a01bf8d28ba0",
      "c055a894-698e-41c0-b85f-7510a7351d9d",
      "b1140fd3-018a-4af5-af4c-b24763335fac",
      "29ea7d0f-1c2b-4bf1-bbaa-e7bfac2d81bd",
      "6e244d8b-db62-4d50-a7fe-79c3d7bf5256"
   ],
   "serverTime": 1564555406117
}

suggestion for json filter

Hello @jason-lynch

There are perfect plugin for json object viewer so good
but sometimes the json is so large
so there are idea for filter text and manage by collapse so here or part of object only show

This is only my suggestion

Thank you

Suggestion for 'copy to clipboard' feature?

I don't suppose there would be any out of the box, minimal way to add a 'copy json to clipboard' feature?

Just looking at the element that is created (#json-renderer), I can see all the data is dispersed throughout subsequent child elements.

So, if I wanted to add this feature, when loading the data, I suppose I would need to add the JSON to a data attribute of an element or perhaps even a cookie, and then implement a standard 'copy to clipboard' method?

Perhaps:

https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard

Below is a little proof of concept - seems to work, has pretty printing and all!

(I'm not sure if there is a size 'limit' on HTML data attributes).

Part 01 - loading the JSON:

// instantiate json-renderer  
$('#json-renderer').jsonViewer(my_json_data, {collapsed: false, rootCollapsable: true, withQuotes: false, withLinks: false});

// prepend an icon to the json-renderer div  
$('#json-renderer').prepend("<span id='copy_to_clipboard' style='position: absolute; right: 50px; margin-top: 10px; cursor: pointer'></span>"); 

// add the json to the icons data attribute   
$('#copy_to_clipboard').data('my_json_data',my_json_data);

Part 02 - copying to the clipboard:

// copy to clipboard    
const copy_to_clipboard = async (my_json_data) => {

try {
    await navigator.clipboard.writeText(JSON.stringify(my_json_data, null, 2));
    console.log('content copied to clipboard!');
} catch (err) {
    console.error('failed to copy: ', err);
}
}

// add click handling 
$(document).on("click", "#copy_to_clipboard", function() {
    let $this = $(this);  
    let my_json_data = $this.data("my_json_data")
    copy_to_clipboard(my_json_data); 

}); 

isUrl function incorrectly detects Url string

function isUrl(string) {
var urlRegexp = /^(https?://|ftps?://)?([a-z0-9%-]+.){1,}([a-z0-9-]+)?(:(\d{1,5}))?(/([a-z0-9-._~:/?#[]@!$&'()*+,;=%]+)?)?$/i;
return urlRegexp.test(string);
}

Since the protocol segment is set to be optional, the function will take strings like ""foo.bar"" as Url. Is this an expected behavior? Why not set the protocol segment required?

withLinks : true causes large performance drop

Hello, thanks a lot for your work. This is a very helpful library!

I work with large JSONs and jsonViewer takes few minutes to finishs its job. Yet, when I use the option withLinks: False, its blazingly fast.

I don't know if it's possible to use lighter regexps, but maybe the simplest way to go would be to use withLinks: False as a default option or just warn the users about this problem.

Option to keep toplevel non-collapsible

I seen you have the code to make the toplevel collapse..

var html = json2html(json, options);
      if (isCollapsable(json))
        html = '<a href class="json-toggle"></a>' + html;

I was was thinking of adding options.toplevelCollapsable != false This will mean if toplevelCollapsable is true or absent, it will still allow collapse, if it's set to false, it will not allow it to collapse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.