Format curl before writing to file

I use curl (7.39.0) to call the REST web service and write the response in a .json file.

That's what I call him

 curl -LX POST -b cookies.txt -H "Content-Type: application/json" http://localhost:7001/web/service/url -d {"param1":"value1"} -o "C:\output\serviceName.json" 

The response is written to the output file, but without formatting.

 {"status": "success","user": "name", "regId": "14420","subscriber": [{"memberFor":"3 years","lastLogin":"2 days ago"}]} 

My questions:

a / Is there a way to format the json response before writing it to the output file (e.g. below)?

 { "status": "success", "user": "name", "regId": "14420" "subscriber": [ { "memberFor":"3 years", "lastLogin":"2 days ago" } ] } 

b / If formatting is not possible via cURL, I want to write a simple batch file to automatically open the json output file and apply formatting. Sort of

 @echo off cls "C:\Program Files\Notepad++\notepad++" "C:\output\serviceName.json" pause 

Are there any flags / options in MS-BATCH for this?

thanks

+1
source share
3 answers

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 // end Batch / begin JScript hybrid chimera var xObj = WSH.CreateObject('Microsoft.XMLHTTP'), fso = WSH.CreateObject('Scripting.FileSystemObject'), temp = WSH.CreateObject('WScript.Shell').Environment('Process')('temp'), j2lib = 'https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js', json = WSH.StdIn.ReadAll(); if (fso.FileExists(temp + '\\json2.js')) { j2lib = fso.OpenTextFile(temp + '\\json2.js', 1); eval(j2lib.ReadAll()); j2lib.Close(); } else { with (xObj) { open("GET", j2lib, true); setRequestHeader('User-Agent', 'XMLHTTP/1.0'); send(''); } while (xObj.readyState != 4) WSH.Sleep(50); eval(xObj.responseText); j2lib = fso.CreateTextFile(temp + '\\json2.js', true); j2lib.Write(xObj.responseText); j2lib.Close(); } WSH.Echo(JSON.stringify(JSON.parse(json), null, '\t')); 

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 // end Batch / begin JScript hybrid chimera var IE = WSH.CreateObject('InternetExplorer.Application'), json = WSH.StdIn.ReadAll(); IE.Visible = 0; IE.Navigate('about:blank'); while (IE.Busy || IE.ReadyState != 4) WSH.Sleep(25); var JSON = IE.document.parentWindow.JSON, pretty = JSON.stringify(JSON.parse(json), null, "\t"); WSH.Echo(pretty); IE.Quit(); try { while (IE && IE.Busy) WSH.Sleep(25); } catch(e) {} 

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 for /f loop forces mshta to communicate with stdout rem // as a console script host. Without for /f, attempting rem // to write to stdout results in an invalid handle error. for /f "delims=" %%I in ('mshta.exe "%~f0"') do echo(%%I goto :EOF end batch / begin HTA : --> <meta http-equiv="x-ua-compatible" content="IE=9" /> <script> var fso = new ActiveXObject('Scripting.FileSystemObject'), stdin = fso.GetStandardStream(0), stdout = fso.GetStandardStream(1), json = stdin.ReadAll(), pretty = JSON.stringify(JSON.parse(json), null, '\t'); close(stdout.Write(pretty)); </script> 

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 // end batch / begin JScript hybrid chimera var htmlfile = WSH.CreateObject('htmlfile'), json = WSH.StdIn.ReadAll(); htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />'); var JSON = htmlfile.parentWindow.JSON, pretty = JSON.stringify(JSON.parse(json), null, '\t'); htmlfile.close(WSH.Echo(pretty)); 
+1
source

Just an update for an old question. https://stedolan.imtqy.com/jq/ is a pretty nice tool. By default, jq pretty-prints JSON output. So you can do

 curl ... | jq . 

"this is an identity filter. See https://stedolan.imtqy.com/jq/manual/#Invokingjq

0
source

I believe that all this can be done with Xidel , which by default is pretty printed:

 xidel -s --method=POST ^ --load-cookies="cookies.txt" ^ -H "Content-Type: application/json" ^ -d {"param1":"value1"} ^ http://localhost:7001/web/service/url ^ -e "$json" 

Expected Result:

 { "status": "success", "user": "name", "regId": "14420" "subscriber": [ { "memberFor":"3 years", "lastLogin":"2 days ago" } ] } 
0
source

Source: https://habr.com/ru/post/956225/


All Articles