0

I have a very huge file in json format where each record end with "}}}," and same goes till last record where the last record comes like "}}}, " which has "comma(,)" and two spaces at end. Now what i want to achieve is the last record should be updated as "}}}" there should be no "comma" and paces coming at last record.

Attach a sample of last record i need this to be done through Bach script. I quick help would be much appreciated !!!

    [{
{"availabilityDate":"2020-10-07","status":"ACT"},"organization":{"id":"TMO1","code":"TMO1","type":"SUP","name":"TMO Process & Data","subType":"DEP",
"status":"ACTIVE","availablityDate":"2020-10-07","orgLevelFromTop":0,"orgLevels":[{"level":"0","orgCode":"TMO1","orgName":"TMO Process & Data"}]},
}}},
{
{"availabilityDate":"2020-10-07","status":"ACT"},"organization":{"id":"TMO1","code":"TMO1","type":"SUP","name":"TMO Process & Data","subType":"DEP",
"status":"ACTIVE","availablityDate":"2020-10-07","orgLevelFromTop":0,"orgLevels":[{"level":"0","orgCode":"TMO1","orgName":"TMO Process & Data"}]},
"job":{"profileId":2423532,"titleOfPosition":"Data Steward","managementLevel":"MG1","familyId":"32532532","familyGroup":"JOB1","familyName":"Data Mgmt",
"familyStatus":"ACT","status":"ACT"},"unionMembership":{"id":"21424221","name":"CAN - WEST - CALGARY"}}},  
]

This is what i tried :-

@echo off
    setlocal enabledelayedexpansion
        for /F %%A in (%newfile) do (
        set mvar=%%A 
        set kvar=!mvar:~1,-1!
        echo !kvar!
        )
New contributor
Rohit Shamdasani is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Do you want a batch to change all json files in a folder like that or just change one file? Cause if it is only one file it really would be easier to change it manually. – Ricardo Bohner Nov 20 at 12:22
  • This may not be possible with batch, depending on your definition of huge. Batch cannot read lines that exceed 8197 bytes in length. It is not unreasonable for a JSON file to exceed that limit. – dbenham 16 hours ago
  • The solution is simple with the hybrid JScript/batch utility JREPL.BAT. call jrepl ", *$" "" /inc -1 /f "yourfile" /o - – dbenham 16 hours ago
0

Maybe something like this:

@echo off
SetLocal EnableDelayedExpansion

:: Put the path or name of old json if in same folder as batch here:
set OldJson=Old.json

:: Put the path or name of new json if in same folder as batch here:
set NewJson=New.json

For /f "usebackq delims=" %%a in ("%OldJson%") do (
echo %%a | find /i "}}}"
IF !errorlevel! EQU 0 set /a Occurances+=1
)

For /f "usebackq delims=" %%a in ("%OldJson%") do (
set "Line=%%a"
set "Line:&=^&"
set "Line:<=^<"
set "Line:>=^>"
set "Linechange=!Line:}}},  =}}}!"
echo %%a | find /i "}}}"
IF !errorlevel! EQU 0 set /a Counter+=1
IF not !Counter! EQU !Occurances! (echo !Line!>>"%NewJson%") else (echo !LineChange!>>"%NewJson%")
)
| improve this answer | |
  • Hi Ricardo, the solution is actually replacing all the occurrence throughout the file but i want it to replace only the last occurrence if suppose there is [{data1},{data2},{data3}, ] so we want only the last {data3}, --> {data3} without "," and spaces. – Rohit Shamdasani Nov 20 at 14:58
  • Ah ok, I thought that only the last occurance could possibly have "}}}, " – Ricardo Bohner Nov 20 at 15:25
  • Please check the changes to my answer... – Ricardo Bohner Nov 20 at 16:19
  • Take the last }}}, does not mean that it occurs in the last }}} – It Wasn't Me 2 days ago
  • @ItWasntMe I have must missed something than, I don't know anything about json files.... – Ricardo Bohner 2 days ago
0
@echo off && cd /d "%~dp0"

setlocal enabledelayedexpansion && set /a "_line=0, _skip=0"
set "_json=Source_File.json" && set "_json_tmp=%temp%\Json_Temp.json"
for /f tokens^=2delims^=^: %%i in ('find/v /c "" "%_json%"')do set/a "_skip=%%i-1"

for /l %%L in (!_skip! -1 1)do for /f tokens^=*delims^=^\* %%A in ('more +%%~L ^<"!_json!"
')do set "_str=%%A" && cmd /v /c "echo=!_str:~-6!|findstr "}}},.."|find ",  "">nul && (
     set/a "_line=%%~L+1" && goto %:^)
    )

%:^)
for /f ^eol^=*^usebackq^tokens^=*delims^=^\* %%A in ("!_json!"
)do set _str=<nul & set "_str=%%A" & >nul set/a "_cnt+=1" && 2>nul (
     >>"!_json_tmp!" (if !_cnt! equ !_line! (echo=!_str:~,-3!
        )else echo=!_str!)
    )

type "!_json_tmp!" >"!_json!" && del/q "!_json_tmp!"
timeout /t -1|type "!_json!" && endlocal & goto :EOF

  • Obs.: Assuming the file_script.bat is in the same folder as your file.json _ _

  • Basically inverts the listing using a numeric loop, where it operates in a negative way, if the file has 10 lines, the loop would start from 10 -1 of 1 in 1, list from the last to the first line, and another for /f loop to check if the line ends with these characters you need to remove:


1. Enter the folder where your .json file is:

... cd /d "%~dp0"

2. For double for loops, you need to enable enabledelayedexpansion:

setlocal enabledelayedexpansion ...

3. Define the values ​​of variables to 0, prevention against possible errors in aborted executions, etc ...

... set /a "_line=0, _skip=0"

rem :: is the same ::
set _line=0
set _skip=0

4. Edit and add your file name, or preferably the full path

set "_json=Source_File.json" ...

5. Only a path and file name for temporary use that will receive the relevant editions:

set "_json_tmp=%temp%\Json_Temp.json" ...

6. To count the lines in the file and set (-1) to start the search for the last occurrence }}}, in order of the last line to the first, searching the last line, second last line, and so on, within the current loop:

for /f tokens^=2delims^=^: %%i in ('find/v /c "" "%_json%"')do set /a "_skip=%%i-1"

7. Use a for /L loop starting in total (from total lines -1), allowing you to scroll through the file from the last line to the first, already using a for /F with [more SKIP %%L], to skip those in the order mentioned above, also to use and define the last line (of the "time") in the _str variable:

for /l %%L in (!_skip! -1 1)do for /f tokens^=*delims^=^\* %%A in ('more +%%~L ^<"!_json!"
')do set "_str=%%A"...

8. Once _str contains the current line, in the reverse order of the lines listed in the file, only the last 6 digits are used to check the first occurrences of the strings to be removed from the line, if fidnstr + find returns 0, you save the line occurrence (last one/line number), and add +1 to this line variable, in order to leave the current loop immediately to make use of that line/number in the label %:^), using with if | else condition:

... cmd /v /c "echo=!_str:~-6!|findstr "}}},.."|find ",  "">nul && (
     set /a "_line=%%~L+1" && goto %:^)
    )

9. Once inside the label, line by line is saved in _str, using for /f, just as a counter (_cnt) is implemented in order to make it possible to filter the line accordingly ...

%:^)
for /f ^eol^=*^usebackq^tokens^=*delims^=^\* %%A in ("!_json!"
)do set _str=<nul & set "_str=%%A" & >nul set/a "_cnt+=1"

10. For each line in this for /F loop, if | else checks whether the line is intended for alterations, and echoes pertinently, subtracting the last 3 (echo=!_str:~,-3!) digits, if the line is different, echoes the current line without changes:

>>"!_json_tmp!" (if !_cnt! equ !_line! (echo=!_str:~,-3!
        )else echo=!_str!)

11. They temporarily file intended to save the lines with/without changes overwrites the files originally at the end of executions:

type "!_json_tmp!" >"!_json!" ...

12. Remove the temporary file, then use a timeout in wait indefinitely for a key press. But redirected to type command of the source file updated file for a brief display of the result on the screen, after the execution is finished.

del/q "!_json_tmp!"
timeout /t -1|type "!_json!" && endlocal & goto :EOF

Obs.: if you don't want this display:

del/q "!_json_tmp!"
timeout /t -1|type "!_json!" &&  endlocal & goto :EOF


Additional resources:

| improve this answer | |

Your Answer

Rohit Shamdasani is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.