JSON file in wrong format.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 3
Joined: 11 Jan 2021 07:49

JSON file in wrong format.

#1 Post by Craig » 21 Jan 2021 15:14

When my batch file downloads this json file it is not "pretty".

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?


Posts: 1042
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: JSON file in wrong format.

#2 Post by ShadowThief » 21 Jan 2021 16:03

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.

Posts: 1936
Joined: 23 Jun 2013 06:15
Location: Germany

Re: JSON file in wrong format.

#3 Post by penpen » 21 Jan 2021 17:20

The following might help you:
- viewtopic.php?f=3&t=9049#p59302
- viewtopic.php?f=3&t=9049#p59297


Posts: 4245
Joined: 22 Jan 2010 18:01
Location: Germany

Re: JSON file in wrong format.

#4 Post by aGerman » 22 Jan 2021 11:25

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.write('<html><head><title></title><meta http-equiv="x-ua-compatible" content="IE=9" /></head></html>');
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));

Post Reply