There are json file with variable quantity of elements describing the keywords. The following is the typical example:
{"keywords":[{"keyword":"halloween","score":0.9621220167107003},
{"keyword":"pumpkin","score":0.9527655356551675},
{"keyword":"nature","score":0.8530320061364176},
{"keyword":"animal","score":0.7936456829239327}],"status":"ok"}
The script should parce this json and I need to get a line with keywords formatted as following:
,,,"halloween,pumpkin,nature,animal"
As I already stated, the number of entries may be different. From 10 to 100, for example.
Honestly, I am stuck with that task. Would anyone help me please?
setlocal enabledelayedexpansion
set keywords=""
rem Loop through each line of the keywords.txt file
for /f "skip=1 tokens=2 delims={" %%b in (keywords.txt) do (
rem Extract the keyword from the current line
set keyword=%%b
set keyword=!keyword:"keyword":"!
set keyword=!keyword:",score":!
set keyword=!keyword:,!
rem Add the keyword to the keywords variable
set keywords=!keywords!,!keyword!
)
But it doesn’t help anything
4
Answers
Note that if the filename does not contain separators like spaces, then both
usebackq
and the quotes around%filename1%
can be omitted.Read each line; replace each colon and various brackets with commas. The result is a simple comma-separated list in
line
. Some elements may be quoted.Use a simple
for
to iterate through all of the elements on each line. If the element stripped of quotes and requoted is"keyword"
then set thesnagnext
flag toy
.The next element to appear when
snagnext
is defined gets accumulated into thewords
list.Tip: Use
set "var=value"
for setting string values – this avoids problems caused by trailing spaces. Don’t assign"
or a terminal backslash or Space. Build pathnames from the elements – counterintuitively, it is likely to make the process easier. If the syntaxset var="value"
is used, then the quotes become part of the value assigned.For parsing JSON, use the help of PowerShell:
Here’s a simple one line batch-file example:
This simpler method combine several conversion steps in lesser commands, so it is more efficient and run faster:
for /F "delims=,"
command eliminates from first comma to end of line, so it just gets{"keywords":[{"keyword":"halloween"
or{"keyword":"pumpkin"
inline
variable."!line:*[=!"
part eliminates from beginning of line until first[
, if any, so it process only the first line giving{"keyword":"halloween"
; that is, the same value of the rest of lines.for /F "tokens=2 delims=:"
command over previous replacement get just the keyword value, like"halloween"
or"pumpkin"
.%%~b
used to add each keyword eliminate the quotes.You’ve included the tag parsing, but as Batch doesn’t support JSON at all, I hope you realize that you can’t use it to parse JSON without resorting to fragile hacks.
I’d recommend you use a dedicated JSON tool instead, like xidel:
And if you really want the surrounding double-quotes, either of these will do:
If it’s simply through a GET- or POST-request (with
curl
?) how you got this JSON in the first place, thenxidel
help out as well. No need to save it to a temporary file.