JSON2Plain
JSON in, Plain text out.
The json2plain module was written as a suitable final fallback for a REST API doing automatic content negotiation.
Installation
$ npm install json2plain
Usage
var json2plain = require('json2plain');
var plain = json2plain(json, options);
Parameters
json
- Can be a JSON Object or valid JSON String.options
- Optional Object, options are listed below.
Simple Example
var json2plain = require('json2plain');
var json = {
"code": 7489394874,
"error": "It don't workie",
"description": "Someone broke it."
};
var plain = json2plain(json);
console.log(plain);
Code: 7489394874
Error: It don't workie
Description: Someone broke it.
More Advanced Example
var json2plain = require('json2plain');
var json = {
hello: "world",
number: 48392,
array: [
'string',
3948484,
true,
'string'
], "object": {
"string": "hello again",
"another": {
"hey": "there"
}
}
};
function ucFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
var options = {
list: '* ',
indent: ' ',
separator: '\t=\t',
formatKey: ucFirst,
formatValue: ucFirst
};
var plain = json2plain(json, options);
console.log(plain);
Hello = World
Number = 48392
Array =
* String
* 3948484
* True
* String
Object =
String = Hello again
Another =
Hey = There
Options
depth
{Number} - amount of indentation to start with, defaults to 1.newline
{String} - string to use for newline, defaults to '\n'.indent
{String} - indentation, defaults to two spaces: ' '.separator
{String} - used to separate key from value, default: ': '.prefix
{String} - inserted before the output, defaults to '\n'.suffix
{String} - appended to the final output, defaults to '\n'.list
{String} - 'numbered' will list numbered array keys, defaults to '- '.formatKey
{Function} - format the keys: function(key){return key.toUpperCase()}..formatVal
{Function} - format the values: function(val) {return val}.
Please see the examples folder for working examples of json2plain in action.
License
(The MIT License)
Copyright (c) 2012 Jarrad Seers <jarrad@jarradseers.com>
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.