-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_edit.bat
151 lines (134 loc) · 5.54 KB
/
json_edit.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@if (@CodeSection == @Batch) @then
:: https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/edit.json.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION&::(Don't pollute the global environment with the following)
::**********************************************************************
SET $NAME=%~n0
SET $DESCRIPTION=Edit JSON and update value of specific key
SET $AUTHOR=Vasil Arnaudov [https://github.com/npocmaka]
SET $SOURCE=%~f0
::@(#)NAME
::@(-) The name of the command or function, followed by a one-line description of what it does.
::@(#) %$NAME% -- %$DESCRIPTION%
::@(#)
::@(#)SYNOPSIS
::@(-) In the case of a command, a formal description of how to run it and what command line options it takes.
::@(-) For program functions, a list of the parameters the function takes and which header file contains its definition.
::@(-)
::@(#) %$NAME% KEY VALUE
::@(#) %$NAME% KEY.SUBKEY VALUE
::@(#)
::@(#)OPTIONS
::@(-) Flags, parameters, arguments (NOT the Monty Python way)
::@(#) -h Help page
::@(#)
::@ (#)
::@(#)DESCRIPTION
::@(-) A textual description of the functioning of the command or function.
::@(#) Update a specific value in a JSON
::@(#)
::@(#)EXAMPLES
::@(-) Some examples of common usage.
::@(#)
::@(#)
::@(#) SET JSON="normal.json"
::@(#) SET JSON_edit="%~f0"
::@(#)
::@(#) ECHO:{ "solo": "Solo value", "level1": { "name": "Name1", "version": "04.21", "level2": { "name": "Name2", "code": "da" } }, "single": "Single value" }>%JSON%
::@(#) ECHO:%JSON%
::@(#) type %JSON%
::@(#) ::{ "solo": "Solo value", "level1": { "name": "Name1", "version": "04.21", "level2": { "name": "Name2", "code": "da" } }, "single": "Single value" }
::@(#) ECHO:Edit: solo=simple solo
::@(#) CALL %JSON_edit% %JSON% solo "simple solo"
::@(#) ::{"solo":"simple solo","level1":{"name":"Name1","version":"04.21","level2":{"name":"Name2","code":"da"}},"single":"Single value"}
::@(#) ECHO: ^^^^^^^^^^^^^^^^^^^^^^
::@(#) ECHO:Edit: level1.name=name of level1
::@(#) CALL %JSON_edit% %JSON% level1.name "name of level1"
::@(#) ::{"solo":"Solo value","level1":{"name":"name of level1","version":"04.21","level2":{"name":"Name2","code":"da"}},"single":"Single value"}
::@(#) ECHO: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::@(#) ECHO:Edit: level1.level2.name=name of level 2
::@(#) CALL %JSON_edit% %JSON% level1.level2.name "name of level 2"
::@(#) ::{"solo":"Solo value","level1":{"name":"Name1","version":"04.21","level2":{"name":"name of level 2","code":"da"}},"single":"Single value"}
::@(#) ECHO: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::@(#)
::@ (#)EXIT STATUS
::@(-) Exit status / errorlevel is 0 if OK, otherwise 1+.
::@ (#)
::@ (#)ENVIRONMENT
::@(-) Variables affected
::@ (#)
::@ (#)
::@ (#)FILES,
::@(-) Files used, required, affected
::@ (#)
::@ (#)
::@ (#)BUGS / KNOWN PROBLEMS
::@(-) If any known
::@ (#)
::@ (#)
::@(#)REQUIRES
::@(-) Dependecies
::@(#)
::@ (#)SEE ALSO
::@(-) A list of related commands or functions.
::@ (#)
::@ (#)
::@ (#)REFERENCE
::@(-) References to inspiration, clips and other documentation
::@ (#) Author:
::@ (#) URL:
::@ (#)
::@(#)
::@(#)SOURCE
::@(-) Where to find this source
::@(#) %$Source%
::@(#)
::@ (#)AUTHOR
::@(-) Who did what
::@ (#) %$AUTHOR%
::*** HISTORY **********************************************************
::SET $VERSION=YYYY-MM-DD&SET $REVISION=hh:mm:ss&SET $COMMENT=Description/init
SET $VERSION=2021-12-28&SET $REVISION=16:19:11&SET $COMMENT=Initial/ErikBachmann
::**********************************************************************
::@(#){COPY}%$VERSION:~0,4% %$Author%
::**********************************************************************
::ENDLOCAL
GOTO :main
SET JSON="\_\examples\testdata\normal.json"
SET JSON_edit="\_\json_edit.bat "
>type %JSON%
{ "solo": "Solo value", "level1": { "name": "Name1", "version": "04.21", "level2": { "name": "Name2", "code": "da" } }, "single": "Single value" }
%JSON_edit% %JSON% solo "simple solo"
{"solo":"simple solo","level1":{"name":"Name1","version":"04.21","level2":{"name":"Name2","code":"da"}},"single":"Single value"}
%JSON_edit% %JSON% level1.name "name of level1"
{"solo":"Solo value","level1":{"name":"name of level1","version":"04.21","level2":{"name":"Name2","code":"da"}},"single":"Single value"}
%JSON_edit% %JSON% level1.level2.name "name of level 2"
{"solo":"Solo value","level1":{"name":"Name1","version":"04.21","level2":{"name":"name of level 2","code":"da"}},"single":"Single value"}
::----------------------------------------------------------------------
:main
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;
//needs file existence checks
var jsloc=WScript.Arguments.Item(0);
var jsonPath=WScript.Arguments.Item(1);
var newValue=WScript.Arguments.Item(2);
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var txtFile=FSOObj.OpenTextFile(jsloc,1);
var json=txtFile.ReadAll();
try {
var jParsed=JSON.parse(json);
}catch(err) {
WScript.Echo("Failed to parse the json content");
htmlfile.close();
txtFile.close();
WScript.Exit(1);
//WScript.Echo(err.message);
}
eval("jParsed."+jsonPath+"=newValue");
WScript.Echo(eval("JSON.stringify(jParsed)"));
htmlfile.close();
txtFile.close();