Edit: I found a solution using the htmlfile COM object, which should offer the fastest performance (for at least one run) and does not require an Internet connection. See the last solution in this answer.
Since you tagged this question with the [batch-file] tag, and since I found it interesting, I wrote a hybrid batch + JScript script that will decorate your JSON. Since JScript 5.7 does not support a JSON object , this script uses external json2.js, loading it through XHR if it is not already loaded. From there, it's just a matter of calling JavaScript the familiar JSON.stringify() method with its beautify variants.
Syntax:
json_generator | batfile.bat -or- batfile.bat < jsonfile.json
Usage example:
beautify.bat < "C:\output\serviceName.json" > "C:\output\beautified.json"
This causes the following to be saved as beautified.json:
{ "status": "success", "user": "name", "regId": "14420", "subscriber": [ { "memberFor": "3 years", "lastLogin": "2 days ago" } ] }
Code:
@if (@CodeSection == @Batch) @then @echo off & setlocal cscript /nologo /e:JScript "%~f0" goto :EOF @end
Here's another solution using the same syntax that doesn't require json2.js to load. This avoids this by starting Internet Explorer silently, invoking IE's built-in JSON methods, and then closing IE again. Most likely, it will be slower than the method above, and it may be blocked depending on the security policies of the machine; but it has the advantage of working without an internet connection.
@if (@CodeSection == @Batch) @then @echo off & setlocal cscript /nologo /e:JScript "%~f0" goto :EOF @end
Here's another solution, this time using the Batch / HTA hybrid. There is a <meta> that makes the HTA interpreter compatible with IE9, including support for JSON methods. This is faster than the IE method, but not completely invisible. The HTA window flashes instantly on the screen, then closes.
<!-- : batch portion @echo off & setlocal rem
The best solution, I think, is to use a leanly documented htmlfile COM object. Using the same trick with the <meta> tag to make it compatible with IE9, as demonstrated above using the HTA solution, the htmlfile COM object offers native support for JSON methods without having to load the library and without marking up an additional window helper expression. It just loads the dll.
@if (@CodeSection == @Batch) @then @echo off & setlocal cscript /nologo /e:JScript "%~f0" goto :EOF @end