PDF download Download Article
Convert any .txt file to .json format easily with our guide
PDF download Download Article

Do you have a TXT file that you need to convert to JSON? If your TXT file is already formatted in JSON syntax, you can convert it simply with a plain text editor, or by using an online conversion tool called MConverter. If your TXT file isn't formatted in JSON syntax yet, you can use a Python code in Visual Studio to convert your data. Keep reading to learn more.

Ways to Convert TXT to JSON

  • Use a text editor to quickly and easily convert TXT files to JSON.
  • Use MConverter online to convert TXT to JSON.
  • If you have data in key-value pairs, you can use Visual Studio .
  • Visual Studio can also be used to convert TXT files with multiple fields to JSON.
Method 1
Method 1 of 4:

Plain Text Editor

PDF download Download Article
  1. 1
    Open your TXT file in Notepad or TextEdit. Notepad and TextEdit are both plain text editors that come pre-installed on Windows (which has Notepad) and Mac (which has TextEdit).
    • You can either open your file directly from File Explorer/Finder, or you can open Notepad/TextEdit and open the file via File > Open .
    • You can also use third-party programs, like Notepad++.
  2. 2
    Save the file as a JSON. Note that your TXT file must be written in the correct syntax for the JSON file to actually work after you save it.
    • Click File > Save as.
    • Give your document a name, being sure to type .json after the document name.
      • For example, if you want to name your document "data," you'd name the file data.json .
    • Save the file. Your text editor will automatically save the file as a JSON file.
  3. Advertisement
Method 2
Method 2 of 4:

MConverter

PDF download Download Article
  1. 1
    Go to MConverter's TXT to JSON converter . MConverter allows you to upload a TXT file and convert it to JSON.
  2. 2
    Click Browse in the middle of the page. You can also drag and drop a TXT file onto the webpage to start the conversion process.
  3. 3
    Choose your TXT file. Navigate to the file and double-click on it to open it up in MConverter.
  4. 4
    Click START . MConverter will have already selected "{...} json" as the target format, so you don't need to change anything on this page.
  5. 5
    Download the JSON file. MConverter will automatically download the JSON file for you once it's done converting, so you don't need to click any buttons to get your file.
    • MConverter will save the file with "_MConverter.eu" in the file name. If you don't want the website's name in the filename, simply edit the file name and delete the "_MConverter.eu" part.
    • Note that your TXT file must be written in the correct syntax for the JSON file to actually work after you save it.
  6. Advertisement
Method 3
Method 3 of 4:

Visual Studio (Key-Value Pairs)

PDF download Download Article
  1. 1
    Write your TXT file as key-value pairs. For this method, you don't have to write your TXT file in JSON syntax. You can simply write your data in key-value pairs. The key should always be one word long, but the value can be more than one word (though one word is preferred). Here's an example document:
    firstname Jane
    lastname Doe
    age 50
    occupation Teacher
  2. 2
    Save your TXT document. Give the document a name that you'll be able to find easily. In this example, we're naming our document data.txt .
    • You can save this document anywhere you like, but make sure it's somewhere easy to remember, such as your Documents folder.
  3. 3
    Open Microsoft Visual Studio. You'll find this program in your Start menu or the Applications folder in Finder.
    • If you don't have Visual Studio, you can download it and start a free trial from visualstudio.microsoft.com/downloads .
    • You must also have Python installed, as well as a Python environment.
  4. 4
    Click Open a local folder . Choose the folder you saved your TXT document in.
  5. 5
    Select the Python environment. At the top of the screen, you'll see a dropdown. Choose a Python environment from the list.
    • If you don't see the environment you want, click Add Environment… > Python installation , then choose the Python environment you want to install. Click Install , then Install again. VS will restart to apply the change.
  6. 6
    Create a new Python file. To do this, right-click in Solution Explorer (on the right-hand side of the screen) and choose Add > Add File . Name the new file something like code.py .
    • If you don't see Solutions Explorer, click View > Solution Explorer or press Ctrl + Alt + L .
  7. 7
    Paste the Python code into your new Python file. Make sure to change the name after "filename" to the name of your TXT file. In the example below, it says data.txt . You can also change the output JSON file name, which is listed after "out_file." In the example below, it says data.json .
     # Python program to convert text 
     # file to JSON 
     import 
     json 
     # the file to be converted to 
     # json format 
     filename 
     = 
     'data.txt' 
     # dictionary where the lines from 
     # text will be stored 
     dict1 
     = 
     {} 
     # creating dictionary 
     with 
     open 
     ( 
     filename 
     ) 
     as 
     fh 
     : 
     for 
     line 
     in 
     fh 
     : 
     # reads each line and trims of extra the spaces 
     # and gives only the valid words 
     command 
     , 
     description 
     = 
     line 
     . 
     strip 
     () 
     . 
     split 
     ( 
     None 
     , 
     1 
     ) 
     dict1 
     [ 
     command 
     ] 
     = 
     description 
     . 
     strip 
     () 
     # creating json file 
     # the JSON file is named as test1 
     out_file 
     = 
     open 
     ( 
     "data.json" 
     , 
     "w" 
     ) 
     json 
     . 
     dump 
     ( 
     dict1 
     , 
     out_file 
     , 
     indent 
     = 
     4 
     , 
     sort_keys 
     = 
     False 
     ) 
     out_file 
     . 
     close 
     () 
    
  8. 8
    Click the green play button at the top of the screen. You can click either the solid green play button that says Current Document (code.py) (or whatever you named your Python file) or the play button with a green outline that says Start Without Debugging .
    • The code will immediately run. You may get a Terminal window that pops up, asking you to press any key to continue.
    • Running the Python file automatically saves it.
  9. 9
    Open the new JSON file in Solution Explorer. After running the Python code, you should see a new JSON file in your project folder with the same name that you designated in the code. When opening the file, you'll see that it contains the data that was in your TXT file, but it has now been converted to JSON syntax. https://www.geeksforgeeks.org/python/convert-text-file-to-json-in-python/
  10. Advertisement
Method 4
Method 4 of 4:

Visual Studio (Multiple Fields)

PDF download Download Article
  1. 1
    Write your TXT file with multiple fields on each line. For this method, you don't have to write your TXT file in JSON syntax. You can write your data with multiple fields on each line, which will eventually become a dictionary when converted to JSON. Here's an example document:
    Jane Doe 50 Teacher
    John Doe 20 Counselor
    George Spelvin 65 Principal
    Georgette Spelvin 34 Receptionist
  2. 2
    Save your TXT document. Give the document a name that you'll be able to find easily. In this example, we're naming our document data.txt .
    • You can save this document anywhere you like, but make sure it's somewhere easy to remember, such as your Documents folder.
  3. 3
    Open Microsoft Visual Studio. You'll find this program in your Start menu or the Applications folder in Finder.
    • If you don't have Visual Studio, you can download it and start a free trial from visualstudio.microsoft.com/downloads .
    • You must also have Python installed, as well as a Python environment.
  4. 4
    Click Open a local folder . Choose the folder you saved your TXT document in.
  5. 5
    Select the Python environment. At the top of the screen, you'll see a dropdown. Choose a Python environment from the list.
    • If you don't see the environment you want, click Add Environment… > Python installation , then choose the Python environment you want to install. Click Install , then Install again. VS will restart to apply the change.
  6. 6
    Create a new Python file. To do this, right-click in Solution Explorer (on the right-hand side of the screen) and choose Add > Add File . Name the new file something like code.py .
    • If you don't see Solutions Explorer, click View > Solution Explorer or press Ctrl + Alt + L .
  7. 7
    Paste the Python code into your new Python file. Make sure to change the name after "filename" to the name of your TXT file. In the example below, it says data.txt . You can also change the output JSON file name, which is listed after "out_file." In the example below, it says data.json .
    • Also, be sure to change the fields after "fields" to the keys that match up with the values in your TXT document. In the example below, the first value is "firstname" so the first key after "fields" is "firstname," etc.
    • Change the "emp" after "sno" to the ID for each dictionary. In the example below, the code will create a dictionary for each employee, so the dictionaries will be named emp1, emp2, etc. You can change this to whatever works best with your dataset.
       # Python program to convert text 
       # file to JSON 
       import 
       json 
       # the file to be converted 
       filename 
       = 
       'data.txt' 
       # resultant dictionary 
       dict1 
       = 
       {} 
       # fields in the sample file 
       fields 
       = 
       [ 
       'firstname' 
       , 
       'lastname' 
       , 
       'age' 
       , 
       'occupation' 
       ] 
       with 
       open 
       ( 
       filename 
       ) 
       as 
       fh 
       : 
       l 
       = 
       1 
       for 
       line 
       in 
       fh 
       : 
       # reading line by line from the text file 
       description 
       = 
       list 
       ( 
       line 
       . 
       strip 
       () 
       . 
       split 
       ( 
       None 
       , 
       4 
       )) 
       # for output see below 
       print 
       ( 
       description 
       ) 
       # for automatic creation of id for each dictionary 
       sno 
       = 
       'emp' 
       + 
       str 
       ( 
       l 
       ) 
       # loop variable 
       i 
       = 
       0 
       # intermediate dictionary 
       dict2 
       = 
       {} 
       while 
       i 
       < 
       len 
       ( 
       fields 
       ): 
       # creating dictionary for each employee 
       dict2 
       [ 
       fields 
       [ 
       i 
       ]] 
       = 
       description 
       [ 
       i 
       ] 
       i 
       = 
       i 
       + 
       1 
       # appending the record of each employee to 
       # the main dictionary 
       dict1 
       [ 
       sno 
       ] 
       = 
       dict2 
       l 
       = 
       l 
       + 
       1 
       # creating json file 
       out_file 
       = 
       open 
       ( 
       "data.json" 
       , 
       "w" 
       ) 
       json 
       . 
       dump 
       ( 
       dict1 
       , 
       out_file 
       , 
       indent 
       = 
       4 
       ) 
       out_file 
       . 
       close 
       () 
      
  8. 8
    Click the green play button at the top of the screen. You can click either the solid green play button that says Current Document (code.py) (or whatever you named your Python file) or the play button with a green outline that says Start Without Debugging .
    • The code will immediately run. You may get a Terminal window that pops up, asking you to press any key to continue.
    • Running the Python file automatically saves it.
  9. 9
    Open the new JSON file in Solution Explorer. After running the Python code, you should see a new JSON file in your project folder with the same name that you designated in the code. When opening the file, you'll see that it contains the data that was in your TXT file, but it has now been converted to JSON syntax. [1]
  10. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Article Summary X

      1. Go to https://anyconv.com/txt-to-json-converter/ in a web browser.
      2. Click Choose File .
      3. Navigate to and double-click to open your TXT file.
      4. Click Convert .
      5. Click Download .

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 82,807 times.

      Is this article up to date?

      Advertisement