Text to JSON: How to Convert Plain Text Data into Valid JSON
Not all data arrives in JSON format. Sometimes you receive data as plain text, a CSV file, key-value pairs, or even a loosely structured document and you need to convert it into valid JSON so your application can use it.
Converting text to JSON correctly means understanding JSON’s structure, following its strict syntax rules, and validating the result before it hits your code. This guide walks you through the most common text-to-JSON conversion scenarios with clear examples, and shows you how to use a json validator online to confirm your converted JSON is correct.
What Does “Text to JSON” Mean?
“Text to JSON” means taking data that is stored in a non-JSON text format and restructuring it as a valid JSON string. The source could be:
- Key-value pairs
name=Alice; age=30; active=true - CSV (Comma-Separated Values)
Alice,30,true - Plain text lists
Apple\nBanana\nCherry - INI / config files
[user]\nname=Alice\nage=30 - Custom text formats from legacy systems
All of these can be represented as JSON you just need to apply the correct structure.
Converting Key-Value Text to JSON
The most common text-to-JSON scenario is converting simple key-value pairs.
Original text:
name=Alice
age=30
email=alice@example.com
active=trueConverted to JSON:
{
"name": "Alice",
"age": 30,
"email": "alice@example.com",
"active": true
}Rules to follow when converting:
- Wrap the whole structure in
{}(it becomes a JSON object) - Every key must be in double quotes:
"name", notname - String values must be in double quotes:
"Alice" - Numbers must NOT be in quotes:
30, not"30" - Booleans must be lowercase without quotes:
true/false, not"true" - Use
:to separate key from value (not=) - Use
,between pairs (except after the last one)
After converting, always paste the result into a json validator online to confirm it is syntactically correct before using it in your application.
Converting CSV to JSON
CSV (Comma-Separated Values) is one of the most common data formats outside of JSON. Converting it to JSON means turning rows into objects and columns into key-value pairs.
Original CSV:
name,age,city,active
Alice,30,New York,true
Bob,25,London,false
Carol,35,Sydney,trueConverted to JSON (array of objects):
[
{
"name": "Alice",
"age": 30,
"city": "New York",
"active": true
},
{
"name": "Bob",
"age": 25,
"city": "London",
"active": false
},
{
"name": "Carol",
"age": 35,
"city": "Sydney",
"active": true
}
]The pattern:
- The first CSV row (headers) becomes the JSON keys
- Each subsequent row becomes a JSON object
{} - All objects are collected into a JSON array
[] - Use appropriate data types numbers and booleans without quotes
Converting a Plain Text List to a JSON Array
Original text:
Apple
Banana
Cherry
MangoConverted to JSON:
["Apple", "Banana", "Cherry", "Mango"]Simple flat lists map directly to JSON arrays. Each item becomes a quoted string (or a number if applicable), separated by commas, wrapped in square brackets.
Converting Nested / Hierarchical Text to JSON
When your text data has a parent-child relationship, convert it to nested JSON objects.
Original text (INI-style config):
[database]
host=localhost
port=5432
name=myapp_db
[cache]
host=localhost
port=6379
ttl=3600Converted to JSON:
{
"database": {
"host": "localhost",
"port": 5432,
"name": "myapp_db"
},
"cache": {
"host": "localhost",
"port": 6379,
"ttl": 3600
}
}Each section header becomes a key whose value is a nested object containing that section’s key-value pairs.
Converting Text to JSON Programmatically
When you need to automate the conversion in your code:
JavaScript key-value string to JSON:
function keyValueToJSON(text) {
const result = {};
const lines = text.trim().split("\n");
lines.forEach((line) => {
const [key, ...rest] = line.split("=");
const value = rest.join("=").trim();
// Detect data types
if (value === "true") result[key.trim()] = true;
else if (value === "false") result[key.trim()] = false;
else if (!isNaN(value) && value !== "") result[key.trim()] = Number(value);
else result[key.trim()] = value;
});
return JSON.stringify(result, null, 2);
}Python CSV to JSON:
import csv
import json
def csv_to_json(csv_text):
reader = csv.DictReader(csv_text.splitlines())
rows = list(reader)
return json.dumps(rows, indent=2)After any programmatic conversion, validate the output with a json validator online before deploying edge cases in your source text can produce invalid JSON.
Common Mistakes When Converting Text to JSON
1. Forgetting to quote string values
Every string value in JSON must be wrapped in double quotes.
// Wrong
{ "name": Alice }
// Correct
{ "name": "Alice" }2. Using the wrong data type for numbers
If a value is a number in your source text, it should be a number in JSON not a string.
// Wrong age is a string
{ "age": "30" }
// Correct age is a number
{ "age": 30 }3. Including special characters without escaping
Certain characters inside JSON strings must be escaped with a backslash.
| Character | Escaped form |
|---|---|
Double quote " | \" |
Backslash \ | \\ |
| Newline | \n |
| Tab | \t |
| Carriage return | \r |
// Wrong unescaped quote breaks the string
{ "note": "He said "hello"" }
// Correct
{ "note": "He said \"hello\"" }4. Trailing comma after the last item
// Wrong
{
"name": "Alice",
"age": 30,
}
// Correct
{
"name": "Alice",
"age": 30
}5. Using single quotes
JSON requires double quotes everywhere. Single quotes are not valid.
// Wrong
{ 'name': 'Alice' }
// Correct
{ "name": "Alice" }Validate Your Converted JSON
After converting any text to JSON manually or programmatically always validate the result before using it. Even small formatting errors will cause your JSON parser to throw an exception.
Our json validator online checks your converted JSON instantly:
- Paste the converted JSON into the tool
- Confirm it is syntactically valid (green) or see the exact error location (red)
- Use the formatted output it is clean, indented, and ready to use
This one extra step takes five seconds and prevents a class of bugs that can take thirty minutes to debug.
Frequently Asked Questions
Is there a tool that converts text to JSON automatically? For structured formats like CSV, there are dedicated converters. For key-value pairs and custom text formats, the conversion usually requires a small script or manual restructuring. After converting, validate with a json validator online.
Can I convert a Word document or PDF to JSON? Not directly unstructured documents do not have a natural JSON mapping. You would need to extract the relevant data fields and map them manually to a JSON structure.
What if my text contains special characters like accented letters? JSON supports Unicode natively. Accented and non-Latin characters are valid in JSON string values without any special escaping (though they can also be represented as Unicode escape sequences like \u00E9 for é).
How do I handle text values that look like numbers but should stay as strings? Wrap them in double quotes in your JSON: "zipCode": "10001". A zip code like 01234 would lose its leading zero if stored as a number store it as a string instead.
What is the difference between text to JSON and text to JSON Schema? Text to JSON means converting data to a JSON format. Text to JSON Schema means creating a schema definition that describes the structure of that JSON field names, types, and validation rules.
Converting text to JSON is a common task in every developer’s workflow. The key is understanding JSON’s strict rules double quotes, correct data types, no trailing commas and validating the result before it reaches your application. Use our free json validator online to check valid JSON online and catch any conversion errors instantly.
Ready to validate your JSON? Use our free json validator online no signup required, no data stored, instant results right in your browser.
Try JSON Validator Online →