Data Format Cheat Sheet

Complete reference guide for JSON, XML, and CSV formats. Syntax rules, examples, and best practices all in one place.

📄 JSON (JavaScript Object Notation)

Syntax Rules

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets [] hold arrays
  • Strings must be in double quotes
  • No trailing commas allowed
  • No comments allowed

Data Types

Valid Types:

  • String: "Hello World"
  • Number: 42, 3.14, -10
  • Boolean: true, false
  • null: null
  • Object: {"key": "value"}
  • Array: [1, 2, 3]

Invalid Types:

  • • undefined
  • • function() {}
  • • Date objects
  • • Single quotes 'text'
  • • Trailing commas
  • • Comments // /* */

Examples

Simple Object:

{
  "name": "John Doe",
  "age": 30,
  "isActive": true,
  "salary": null
}

Array of Objects:

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "roles": ["admin", "user"]
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "roles": ["user"]
    }
  ]
}

Best Practices

✅ Do:

  • • Use camelCase for property names
  • • Keep nesting levels reasonable (≤3-4)
  • • Use arrays for lists of similar items
  • • Validate JSON before parsing
  • • Use meaningful property names

❌ Don't:

  • • Use single quotes for strings
  • • Add trailing commas
  • • Include comments
  • • Use undefined values
  • • Create circular references

📋 XML (eXtensible Markup Language)

Syntax Rules

  • All XML documents must have a root element
  • XML tags are case-sensitive
  • All XML elements must be properly closed
  • XML elements must be properly nested
  • XML attribute values must be quoted
  • XML prolog is optional but recommended

Structure Components

Elements:

<element>content</element>
<empty-element/>
<parent>
  <child>value</child>
</parent>

Attributes:

<element attr="value">
<user id="123" active="true">
<img src="image.jpg" alt="Photo"/>

Examples

Simple XML Document:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>123</id>
  <name>John Doe</name>
  <email>[email protected]</email>
  <isActive>true</isActive>
</user>

XML with Attributes:

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user id="1" status="active">
    <name>John Doe</name>
    <role>admin</role>
  </user>
  <user id="2" status="inactive">
    <name>Jane Smith</name>
    <role>user</role>
  </user>
</users>

Special Characters & Entities

Character Entities:

  • • &lt; for <
  • • &gt; for >
  • • &amp; for &
  • • &quot; for "
  • • &apos; for '

CDATA Sections:

<![CDATA[
  <script>alert('Hello!');</script>
]]>

Best Practices

✅ Do:

  • • Use meaningful element names
  • • Keep consistent naming conventions
  • • Use attributes for metadata
  • • Validate against schemas
  • • Include XML declaration

❌ Don't:

  • • Use spaces in element names
  • • Leave elements unclosed
  • • Mix upper and lower case randomly
  • • Use reserved XML names
  • • Forget to escape special characters

📊 CSV (Comma-Separated Values)

Format Rules

  • Fields are separated by commas (or other delimiters)
  • Records are separated by line breaks
  • First row can contain column headers
  • Fields containing commas must be quoted
  • Quotes inside fields must be escaped
  • Leading/trailing spaces are significant

Common Delimiters

By Region:

  • US/UK: Comma (,)
  • Europe: Semicolon (;)
  • Universal: Tab (\t)
  • Special: Pipe (|)

When to Use:

  • Comma: Standard CSV
  • Semicolon: European Excel
  • Tab: Data with commas
  • Pipe: Complex text data

Examples

Basic CSV:

Name,Age,City
John Doe,30,New York
Jane Smith,25,Los Angeles
Bob Johnson,35,Chicago

CSV with Quotes:

Name,Description,Price
"John's Product","A product with, comma",29.99
"Jane's Item","She said ""Hello""",19.99
"Bob's Tool","Simple description",39.99

Quoting Rules

Must Quote When:

  • • Field contains delimiter
  • • Field contains line breaks
  • • Field contains double quotes
  • • Field starts/ends with spaces

Escape Quotes:

"He said ""Hello""" → He said "Hello"
"Text with ""quotes"" inside" → Text with "quotes" inside

Best Practices

✅ Do:

  • • Use consistent delimiters
  • • Include header row
  • • Quote fields with special characters
  • • Use UTF-8 encoding
  • • Validate before import

❌ Don't:

  • • Mix different delimiters
  • • Leave fields unquoted when needed
  • • Use inconsistent line endings
  • • Include BOM unless required
  • • Forget to escape quotes

⚠️ Common Issues & Solutions

Character Encoding

Garbled characters, especially accented letters

Solution: Use UTF-8 encoding and include BOM for Excel compatibility

Parsing Errors

Syntax errors in JSON/XML parsing

Solution: Validate syntax before processing, use proper escaping

Data Type Loss

Numbers become text, dates not recognized

Solution: Use proper data types, validate during conversion

Import Failures

Files won't import into Excel/databases

Solution: Check delimiter settings, file encoding, and format compliance

💡 Quick Tips

Always Validate

Use validators before processing data to catch errors early

Test Small First

Test with sample data before processing large files

Know Your Tools

Understand your target application's format requirements