Tom
Tom

Reputation: 6707

How to convert JSON syntax to another one with a cmd.exe batch file (for loop)

I have 10000 tiny tmp/ txt files that look like these:

1.tmp: {"a": "you","data": "1","data2": "2"} <linefeed> {"b":"bo"}
2.tmp: {"a": "you2","data": "1","data2": "2"} <linefeed> {"b":"bo2"}
3.tmp: {"a": "you3","data": "1","data2": "2"} <linefeed> {"b":"bo3"}

How can I read each of them and convert these to another format, one file that has 1 row per file:

{ "a": "you", "b": "bo" },
{ "a": "you2", "b": "bo2" },
{ "a": "you3", "b": "bo3" },

The tricky part might be that each .tmp file has a linefeed?

My code starts

for /L %%i in (0,1,10000) do (call parsesomehow %%i.tmp )

Upvotes: 0

Views: 152

Answers (1)

Daemon-5
Daemon-5

Reputation: 251

As I understood there were only 2 lines in every tmp file. You have to read every tmp file. In every cycle you should parse 1-st string. Then - 2-nd. This parce cycle must be the stored procedure

Next step is print parsed values to output file.

@echo off
for %%i in (*.tmp) do call :parse %%i
goto :EOF

:parse
for /f "delims=," %%a in (%1) do set "A=%%a" &goto NXT
:NXT
for /f "delims={" %%a in ('more +1 %1') do set "B=%%a"
echo %A%, %B%>>output.txt

Upvotes: 1

Related Questions