Format string to a json like template
<script src="https://unpkg.com/[email protected]/format2json.min.js"></script>
<script>
const source = `{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http:https://zjson.net","project":"http:https://github.com/CN-Tower/zjson","language":["中文(简体)","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}`;
const jsonLike = await format2json(source, { resultOnly: true });
console.log(jsonLike);
</script>
Run: npm install format-to-json --save
;
const format2json = require('format-to-json');
const source = '{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http:https://zjson.net","project":"http:https://github.com/CN-Tower/zjson","language":["中文(简体)","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}';
fmtInfo = await format2json(source);
console.log(fmtInfo.result);
Result:
{
"zjson": "ZJSON",
"description": "Online json formatter",
"version": "v4.1.8",
"updateTime": "2018-11-23",
"url": "http:https://zjson.net",
"project": "http:https://github.com/CN-Tower/zjson",
"language": [
"中文(简体)",
"English"
],
"keywords": [
"zjson",
"json formatter"
],
"content": {
"array": [
"element 001",
"element 002"
],
"boolean": true,
"null": null,
"number": 123,
"string": "Hello World",
"object": {
"property": "value",
"key": "val"
}
}
}
format2json(source: string, options?: FormatOptions): Promise<FormatResult | string>;
interface FormatOptions {
indent?: number; // Integer, Large then 0, default: 2
isExpand?: boolean; // Default: true
isStrict?: boolean; // Default: false
isEscape?: boolean; // Default: false
isUnscape?: boolean; // Default: false
keyQtMark?: "'" | "\"" | ""; // Default: "\""
valQtMark?: "'" | "\""; // Default: "\""
}
// If `{ resultOnly: true }` in option,
// Just eturn the format result string.
interface FormatResult {
result: string;
status: {
fmtType: 'info' | 'success' | 'warning' | 'danger';
fmtSign: 'ost' | 'col' | 'val' | 'end' | 'war' | 'scc' | 'err';
fmtLines: number;
message: string;
errFormat: boolean;
errIndex: number;
errExpect: string;
errNear: string;
}
}
Run: npm install -g format-to-json
Run: format2json -h
Usage: format2json [options]
Options:
-V, --version output the version number
-i, --indent [indent] Indnet for the format.
-k, --keyQtMark [keyQtMark] Key quotation mark.
-v, --valQtMark [valQtMark] Value quotation mark.
-S, --isStrict Strict format to a JSON template.
-U, --isUnescape Unescape the source.
-R, --resultOnly Print the format result only.
-h, --help output usage information
Run: format2json -i 4 -k "" -v "'"
√ Input a string to foramt: · [{name: "Tom", age: 28, gender: "male"}]
==================================================================
[10:42:20] format-to-json(1.0.3)
------------------------------------------------------------------
[
{
name: 'Tom',
age: 28,
gender: 'male'
}
]
------------------------------------------------------------------
{ fmtType: 'success',
fmtSign: 'scc',
fmtLines: 8,
message: 'Success formated 8 lines!',
errFormat: false,
errIndex: NaN,
errExpect: '',
errNear: '' }
==================================================================