Convert text file to json file using batch script

2 min read 24-10-2024
Convert text file to json file using batch script

When it comes to handling data formats in programming, JSON (JavaScript Object Notation) has become a popular choice due to its readability and lightweight nature. However, converting a text file to a JSON file can be a bit tricky, especially for those unfamiliar with scripting. In this article, we will explore how to easily convert a text file to JSON format using a batch script.

Problem Scenario

The task at hand involves taking a text file that contains data and converting it into a properly formatted JSON file using a batch script. The original code for this conversion may appear cumbersome or unclear to those not well-versed in scripting.

Original Batch Script Code

@echo off
setlocal enabledelayedexpansion

set "inputFile=input.txt"
set "outputFile=output.json"

echo [ > "!outputFile!"
for /f "usebackq delims=" %%a in ("!inputFile!") do (
    echo    "%%a", >> "!outputFile!"
)
echo ] >> "!outputFile!"
endlocal

Understanding the Code

In this script:

  1. Setting the Input and Output File: The script sets the variables inputFile and outputFile, specifying the names of the files to read from and write to, respectively.

  2. Initializing the Output File: The script begins the output file with an opening square bracket, which is typical in JSON array notation.

  3. Reading the Input File: The for /f command reads the contents of the input.txt file line by line. Each line is echoed into the output file, surrounded by quotation marks and followed by a comma.

  4. Finalizing the JSON Array: Finally, a closing bracket is appended to the output file to complete the JSON array structure.

Improvements and Clarifications

To make this batch script more effective and ensure valid JSON formatting, here are a few improvements:

  1. Remove the Trailing Comma: If the last line of the JSON does not require a comma, the script should be adjusted to avoid adding it after the last entry.

  2. Dynamic Key-Value Pairs: If you want to create key-value pairs from the text file, you'll need to modify the script accordingly.

Enhanced Batch Script

Here's an improved version of the original script that removes the trailing comma:

@echo off
setlocal enabledelayedexpansion

set "inputFile=input.txt"
set "outputFile=output.json"

echo [ > "!outputFile!"
set "firstLine=1"

for /f "usebackq delims=" %%a in ("!inputFile!") do (
    if !firstLine! equ 1 (
        echo    "%%a" >> "!outputFile!"
        set "firstLine=0"
    ) else (
        echo    ,"%%a" >> "!outputFile!"
    )
)
echo ] >> "!outputFile!"
endlocal

Explanation of Changes

  • Conditional Check for First Line: The addition of firstLine helps in determining if the current line is the first one. It will not prepend a comma before the first line, thus ensuring valid JSON syntax.

Practical Example

Let’s assume your input.txt file contains the following lines:

Apple
Banana
Cherry

After running the above batch script, your output.json file will contain:

[
    "Apple",
    "Banana",
    "Cherry"
]

Conclusion

Converting a text file to JSON using a batch script can significantly streamline your data handling processes. By understanding and modifying the initial script, you can achieve a valid JSON format tailored to your specific needs.

Additional Resources

By leveraging the power of batch scripting and following these guidelines, you can efficiently convert text files to JSON and enhance your data management capabilities.