Page 1 of 1

JSON file in wrong format.

Posted: 21 Jan 2021 15:14
by Craig
When my batch file downloads this json file it is not "pretty".

@ECHO OFF
ECHO. %~n0: Getting JSON files from Web page.
SETLOCAL EnableDelayedExpansion
curl -o Ports.json http://storage.googleapis.com/nacleanop ... odeu1.json

However when I view it using a web based viewer (below), it looks great in the web viewer.
http://jsonviewer.stack.hu/#http://stor ... odeu1.json

What can I use after my curl command to make the downloaded file look like it does in the web viewer?

Craig

Re: JSON file in wrong format.

Posted: 21 Jan 2021 16:03
by ShadowThief
The web viewer is manipulating the data to make the JSON more readable. Curl just grabs the data as it is currently stored. If you want to prettify it, you'll have to do that yourself.

Re: JSON file in wrong format.

Posted: 21 Jan 2021 17:20
by penpen
The following might help you:
- viewtopic.php?f=3&t=9049#p59302
- viewtopic.php?f=3&t=9049#p59297


penpen

Re: JSON file in wrong format.

Posted: 22 Jan 2021 11:25
by aGerman
The outdated version of cURL that ships with Win 10 doesn't support JSON formatting. However the Google API doesn't even return valid JSON. It's rather a JavaScript assignment statement which begins whith var Ports = and ends with a semicolon. You would have to remove that anyway before you can pretty print it. (Also the online viewer is confused and concatenates the first two words to varPorts.)
Besides of that, the oportunities that penpen linked should work.
Tested script:

Code: Select all

@if (0)==(0) echo off
curl -s http://storage.googleapis.com/nacleanopenworldprodshards/Ports_cleanopenworldprodeu1.json | cscript //nologo //e:jscript "%~fs0" >"Ports.json"
goto :eof @end

var objHtml = new ActiveXObject('HTMLFile'), str = WScript.StdIn.ReadAll();
objHtml.open();
objHtml.write('<html><head><title></title><meta http-equiv="x-ua-compatible" content="IE=9" /></head></html>');
objHtml.close();
str = str.replace('var Ports = ', '').slice(0, -1); // remove the assignment and the trailing semicolon
WScript.Echo(objHtml.parentWindow.JSON.stringify(objHtml.parentWindow.JSON.parse(str), null, 2));
Steffen