Blog Productivity Tools Text to JSON: How to Convert R...
Text to JSON: How to Convert Raw Data Into Structured JSON (With Examples)
Productivity Tools Mar 29, 2026 5 min read 215 views

Text to JSON: How to Convert Raw Data Into Structured JSON (With Examples)

APIs expect JSON. Your data is in a text file. Here is how to bridge the gap, with specific examples for common formats like CSV rows, key-value pairs, and line-separated lists.

R
Robert
Author

You have a list of product names in a text file. Or a spreadsheet export with tab-separated values. Or a configuration file with key-value pairs separated by equals signs. And you need this data as JSON because that is what the API, the database import tool, or the frontend application expects.

The conversion itself takes seconds. The part that trips people up is understanding which JSON structure matches their data and how the conversion handles edge cases like special characters, empty values, and data types.

What JSON Actually Is (and Why Everything Uses It)

JSON (JavaScript Object Notation) became the standard data format for web APIs, configuration files, and data exchange because it is both human-readable and machine-parseable. Almost every programming language has built-in JSON support. Every major database can import it. Every API speaks it.

At its core, JSON has two structures. An array is an ordered list: ["red", "green", "blue"]. An object is a set of named values: {"color": "red", "hex": "#FF0000"}. That is it. Everything else is combinations of these two structures.

Developer working with code on monitor

When you convert text to JSON, you are deciding which structure fits your data and how the text maps into it.

Common Conversions With Examples

Line-separated list to JSON array. This is the simplest and most frequent conversion. You have items on separate lines, and you need a JSON array.

Input:

Tokyo
Paris
New York
London

Output:

["Tokyo", "Paris", "New York", "London"]

You can convert text lists to JSON format by selecting "new line" as the delimiter. Duplicates can be removed automatically if you only need unique values.

CSV rows to array of objects. When your text has headers and data rows separated by commas or tabs, each row becomes a JSON object with the headers as keys.

Input:

name,email,role
Sarah,[email protected],engineer
Mike,[email protected],designer

Output:

[
  {"name": "Sarah", "email": "[email protected]", "role": "engineer"},
  {"name": "Mike", "email": "[email protected]", "role": "designer"}
]

Key-value pairs to JSON object. Configuration files and settings often use key=value or key:value format.

Input:

database_host=localhost
port=5432
database_name=production
timeout=30

Output:

{
  "database_host": "localhost",
  "port": "5432",
  "database_name": "production",
  "timeout": "30"
}

Notice that the port and timeout values are strings, not numbers. Text converters typically output everything as strings unless they have type detection. If you need numeric types, either use a converter with auto-detection or parse the values in your application code.

The Gotchas That Break Your JSON

Data on computer screen

Valid JSON is strict. These mistakes produce output that looks right but fails when parsed:

Trailing commas. JavaScript allows a comma after the last item in an array: ["a", "b",]. JSON does not. This is probably the most common cause of "invalid JSON" errors. Remove the trailing comma and it parses fine.

Single quotes. JavaScript lets you use single or double quotes for strings. JSON requires double quotes. {'name': 'John'} is invalid JSON. {"name": "John"} is correct.

Unescaped special characters. If your text contains double quotes, backslashes, or control characters (tabs, newlines), they must be escaped in the JSON string. A product description containing The 12" model needs the quote escaped: "The 12\" model". Converter tools handle this automatically, but manual conversion often misses it.

Inconsistent delimiters. If your text file uses commas in some rows and tabs in others, the converter cannot reliably split the data. Clean up your input to use a consistent delimiter before converting.

Encoding issues. Accented characters (Café), CJK characters, and emoji work fine in JSON (it uses UTF-8), but some older text files use Latin-1 or Windows-1252 encoding. If you see garbled characters in the output, check your source file encoding.

Data Types: String vs. Number vs. Boolean

JSON supports five data types: strings (text in quotes), numbers (no quotes), booleans (true/false, no quotes), null, and nested structures (arrays/objects). The distinction matters because APIs and databases treat them differently.

Text Input As String As Typed Value Which to Use
42 "42" 42 Number if you will do math; string for IDs and codes
true "true" true Boolean for flags and toggles
3.14 "3.14" 3.14 Number for calculations; string for display values
(empty) "" null Depends on whether your system distinguishes empty from absent

A zip code like "07102" should always be a string. As a number, it becomes 7102 and the leading zero disappears. Phone numbers, postal codes, and product SKUs that start with zero need string handling.

When to Use a Converter vs. Writing a Script

Use an online converter when:

  • You have a one-off conversion task
  • The data is flat (no complex nesting needed)
  • The file is small enough to paste into a browser
  • You need results in 30 seconds, not 30 minutes

Write a script when:

  • You convert the same format repeatedly
  • The data needs transformation during conversion (calculations, lookups, validation)
  • The file is too large for browser-based tools
  • You need nested structures that require custom mapping logic

For most developers, the tipping point is about three conversions. If you convert the same format three times, the fourth time you should write a script. Python's json module and JavaScript's JSON.stringify make this trivial.

Validate Before You Send

One piece of advice that saves hours of debugging: always validate your JSON before sending it to an API or importing it into a database. Paste the output into a JSON validator. The validator will flag structural errors, missing quotes, and trailing commas immediately. Catching a syntax error in a validator takes 5 seconds. Debugging a cryptic API error response caused by malformed JSON takes 45 minutes.

The data is already in your text file. The target system wants JSON. The conversion itself is mechanical. Get the structure right, handle the edge cases, validate the output, and you are done.