#1 01 Aug 2017 03:55

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

how to rewrite a really long single .json file

I have a super one line long string from a .json file

{"preventUpnDetectSso":false,"silentUpdateTriggered":false,"rendererCrashInfo":{"datetime":0,"session":"","desktopSession":"","type":""},"previousCrashesInfo":{"crashes":[]},"windowState":{"x":117,"y":70,"width":1280,"height":750,"isMaximized":false,"isFullScreen":false},"restartCommand":{},"userUpn":"","userOid":"41787723-7634-4ef9-93a6-f7222cb1e8cc","userTid":"af42ea71-352f-4820-8b42-b674ab60926c","guestTenantId":"","launchTime":"1501553462308","desktopZoomLevel":0,"overrideOpenAsHiddenProperty":true,"desktopSessionId":"desktop-151b82bc-3411-4d73-8386-5b846e03461b","machineId":"8adeaf89f6a52043e3f9314aa8880819b157ca2dba469a9da7bb10d35d7f384e","deviceInfoId":"bfe8de30fa0d1c1714913e8ac3699b341228108d7aa7358f9aa612fa8e74b2b3","restartReason":null,"appPreferenceSettings":{"openAtLogin":true,"openAsHidden":false,"runningOnClose":true},"currentWebLanguage":"en-US","isAppFirstRun":false,"isLoggedOut":false,"isForeground":false,"notificationWindowOnClose":true}

I am trying to use a for loop for get the value - "openAsHidden":false and then rewrite the entire string back changing it to - "openAsHidden":true

So far there are not enough letters in the alphabet to process the string in a for loop, eg

  for /f "usebackq tokens=1-26* delims=," %%a in ("longstring.json") do (
  
  echo A - %%a
  echo B - %%b
  echo C - %%c
  echo D - %%d
  echo E - %%e
  echo F - %%f
  echo G - %%g
  echo H - %%h
  echo I - %%i
  echo J - %%j
  echo K - %%k
  echo L - %%l
  echo M - %%m
  echo N - %%n
  echo O - %%o
  echo P - %%p
  echo Q - %%q
  echo R - %%r
  echo S - %%s
  echo T - %%t
  echo U - %%u
  echo V - %%v
  echo W - %%w
  echo X - %%x
  echo Y - %%y
  echo Z - %%z
  
  )

outputs

A - {"preventUpnDetectSso":false
B - "silentUpdateTriggered":false
C - "rendererCrashInfo":{"datetime":0
D - "session":""
E - "desktopSession":""
F - "type":""}
G - "previousCrashesInfo":{"crashes":[]}
H - "windowState":{"x":117
I - "y":70
J - "width":1280
K - "height":750
L - "isMaximized":false
M - "isFullScreen":false}
N - "restartCommand":{}
O - "userUpn":""
P - "userOid":"41787723-7634-4ef9-93a6-f7222cb1e8cc"
Q - "userTid":"af42ea71-352f-4820-8b42-b674ab60926c"
R - "guestTenantId":""
S - "launchTime":"1501553462308"
T - "desktopZoomLevel":0
U - "overrideOpenAsHiddenProperty":true
V - "desktopSessionId":"desktop-151b82bc-3411-4d73-8386-5b846e03461b"
W - "machineId":"8adeaf89f6a52043e3f9314aa8880819b157ca2dba469a9da7bb10d35d7f384e"
X - "deviceInfoId":"bfe8de30fa0d1c1714913e8ac3699b341228108d7aa7358f9aa612fa8e74b2b3"
Y - "restartReason":null
Z - "appPreferenceSettings":{"openAtLogin":true

This being the case there are not enough letters to contain the whole string, so it cannot be written back to a single string again eg

echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z >longstring.json

Additionally this json file is not static, eg the order may change, so I will have to test every value if it matches the pattern - "openAsHidden":false
Is there a way to process this string using some other trick?

Thanks

Last edited by NDog (01 Aug 2017 04:05)


cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 01 Aug 2017 09:57

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 430

Re: how to rewrite a really long single .json file

Try this (you'll have to pass the json file location as an argument):

@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');
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
var JSON = htmlfile.parentWindow.JSON;

var jsloc=WScript.Arguments.Item(0);
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var txtFile=FSOObj.OpenTextFile(jsloc,1);
var json=txtFile.ReadAll();

var jParsed=JSON.parse(json);


jParsed.appPreferenceSettings.openAsHidden=true;


s = JSON.stringify(jParsed);
WScript.Echo(s);

Offline

#3 01 Aug 2017 09:58

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,011
Website

Re: how to rewrite a really long single .json file

There are some techniques for allowing more than 26 FOR parameters which you can read about over on the DOS Tips forum: http://www.dostips.com/forum/viewtopic. … 660#p51660

In this case though I think you could just capture everything with %* and then do the replacement, because it looks like a unique string:

Set everything=%*
Set new=%everything:%openAsHidden":false=openAsHidden":true%
Echo %new% >new.json

Offline

#4 01 Aug 2017 15:14

Aacini
Member
Registered: 05 Dec 2012
Posts: 143

Re: how to rewrite a really long single .json file

This Batch file do exactly what you requested: change "openAsHidden":false by "openAsHidden":true

@echo off
setlocal

for /F "delims=" %%a in (input.txt) do set "json=%%a"
echo %json:"openAsHidden":false="openAsHidden":true%

Antonio

Offline

#5 01 Aug 2017 21:51

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Re: how to rewrite a really long single .json file

Hi Thanks for your replies

Aacini achieved in two lines what tried to do in about 50 lines
I still can't understand how the echo%% command works, is there any additional reading on this?

Thanks


cmd, vbs, ps, bash
autoit, python, swift

Offline

#6 01 Aug 2017 22:06

Aacini
Member
Registered: 05 Dec 2012
Posts: 143

Re: how to rewrite a really long single .json file

This solution bear no relation to echo command, but to substring replacement that is a basic feature of Batch files:

set "var=One Two Three Four Five"

rem Change "Three" by "Other":
echo %var:Three=Other%

The result is:

One Two Other Four Five

Antonio

Offline

#7 03 Aug 2017 06:48

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Re: how to rewrite a really long single .json file

Thanks!


cmd, vbs, ps, bash
autoit, python, swift

Offline

Board footer

Powered by FluxBB