w

Ejemplos

Ejemplos practicos que muestran distintos casos de uso del convertidor de JSON a CSV.

Ejemplos basicos

Conversion simple de arreglos

JSON de entrada:

[
  { "name": "Alice", "age": 28, "city": "New York" },
  { "name": "Bob", "age": 32, "city": "Los Angeles" },
  { "name": "Charlie", "age": 25, "city": "Chicago" }
]

CSV de salida:

name,age,city
Alice,28,New York
Bob,32,Los Angeles
Charlie,25,Chicago

Conversion de un solo objeto

JSON de entrada:

{
  "product": "Laptop",
  "price": 1299.99,
  "category": "Electronics",
  "inStock": true,
  "tags": ["portable", "gaming", "work"]
}

CSV de salida:

product,price,category,inStock,tags
Laptop,1299.99,Electronics,true,"[""portable"",""gaming"",""work""]"

Ejemplos reales

Datos de productos de comercio electronico

JSON de entrada (catalogo de productos):

[
  {
    "id": "P001",
    "name": "Wireless Headphones",
    "brand": "TechSound",
    "price": 199.99,
    "currency": "USD",
    "availability": {
      "inStock": true,
      "quantity": 150
    },
    "specifications": {
      "battery": "20 hours",
      "connectivity": "Bluetooth 5.0",
      "weight": "250g"
    },
    "reviews": {
      "average": 4.5,
      "count": 128
    }
  },
  {
    "id": "P002",
    "name": "Smart Watch",
    "brand": "FitTech",
    "price": 299.99,
    "currency": "USD",
    "availability": {
      "inStock": false,
      "quantity": 0
    },
    "specifications": {
      "battery": "7 days",
      "connectivity": "Bluetooth, WiFi",
      "weight": "45g"
    },
    "reviews": {
      "average": 4.2,
      "count": 89
    }
  }
]

CSV de salida (aplanado):

id,name,brand,price,currency,availability.inStock,availability.quantity,specifications.battery,specifications.connectivity,specifications.weight,reviews.average,reviews.count
P001,Wireless Headphones,TechSound,199.99,USD,true,150,20 hours,Bluetooth 5.0,250g,4.5,128
P002,Smart Watch,FitTech,299.99,USD,false,0,7 days,"Bluetooth, WiFi",45g,4.2,89

Datos de respuesta de API

JSON de entrada (API de gestion de usuarios):

{
  "users": [
    {
      "id": 1,
      "username": "john_doe",
      "email": "john@example.com",
      "profile": {
        "firstName": "John",
        "lastName": "Doe",
        "avatar": "https://example.com/avatars/john.jpg"
      },
      "permissions": ["read", "write"],
      "lastLogin": "2024-01-15T10:30:00Z",
      "active": true
    },
    {
      "id": 2,
      "username": "jane_smith",
      "email": "jane@example.com",
      "profile": {
        "firstName": "Jane",
        "lastName": "Smith",
        "avatar": "https://example.com/avatars/jane.jpg"
      },
      "permissions": ["read"],
      "lastLogin": "2024-01-14T15:45:00Z",
      "active": true
    }
  ],
  "total": 2,
  "page": 1,
  "limit": 10
}

CSV de salida (solo el arreglo users):

id,username,email,profile.firstName,profile.lastName,profile.avatar,permissions,lastLogin,active
1,john_doe,john@example.com,John,Doe,https://example.com/avatars/john.jpg,"[""read"",""write""]",2024-01-15T10:30:00Z,true
2,jane_smith,jane@example.com,Jane,Smith,https://example.com/avatars/jane.jpg,"[""read""]",2024-01-14T15:45:00Z,true

Ejemplos avanzados

Datos financieros

JSON de entrada (datos del mercado bursatil):

[
  {
    "symbol": "AAPL",
    "company": "Apple Inc.",
    "sector": "Technology",
    "price": {
      "current": 175.43,
      "open": 174.2,
      "high": 176.15,
      "low": 173.5,
      "previousClose": 174.2
    },
    "volume": 45678900,
    "marketCap": 2750000000000,
    "dividend": {
      "yield": 0.0044,
      "amount": 0.24,
      "frequency": "quarterly"
    },
    "analysts": {
      "rating": "Buy",
      "targetPrice": 185.0,
      "recommendations": {
        "strongBuy": 12,
        "buy": 8,
        "hold": 3,
        "sell": 1,
        "strongSell": 0
      }
    }
  }
]

CSV de salida:

symbol,company,sector,price.current,price.open,price.high,price.low,price.previousClose,volume,marketCap,dividend.yield,dividend.amount,dividend.frequency,analysts.rating,analysts.targetPrice,analysts.recommendations
AAPL,Apple Inc.,Technology,175.43,174.20,176.15,173.50,174.20,45678900,2750000000000,0.0044,0.24,quarterly,Buy,185.00,"{""strongBuy"":12,""buy"":8,""hold"":3,""sell"":1,""strongSell"":0}"

Datos de encuestas

JSON de entrada (encuesta de satisfaccion del cliente):

[
  {
    "respondentId": "R001",
    "timestamp": "2024-01-15T14:30:00Z",
    "demographics": {
      "age": "25-34",
      "gender": "Female",
      "location": "California"
    },
    "responses": {
      "satisfaction": 4,
      "recommendation": 5,
      "comments": "Great product, excellent customer service!"
    },
    "metadata": {
      "surveyVersion": "v2.1",
      "completionTime": 180,
      "device": "mobile"
    }
  },
  {
    "respondentId": "R002",
    "timestamp": "2024-01-15T16:45:00Z",
    "demographics": {
      "age": "35-44",
      "gender": "Male",
      "location": "Texas"
    },
    "responses": {
      "satisfaction": 3,
      "recommendation": 3,
      "comments": "Product is okay, but could be better."
    },
    "metadata": {
      "surveyVersion": "v2.1",
      "completionTime": 120,
      "device": "desktop"
    }
  }
]

CSV de salida:

respondentId,timestamp,demographics.age,demographics.gender,demographics.location,responses.satisfaction,responses.recommendation,responses.comments,metadata.surveyVersion,metadata.completionTime,metadata.device
R001,2024-01-15T14:30:00Z,25-34,Female,California,4,5,"Great product, excellent customer service!",v2.1,180,mobile
R002,2024-01-15T16:45:00Z,35-44,Male,Texas,3,3,"Product is okay, but could be better.",v2.1,120,desktop

Manejo de caracteres especiales

Datos internacionales

JSON de entrada (contenido multilingue):

[
  {
    "id": 1,
    "title": "Café & Restaurant",
    "description": "Un café français avec une cuisine délicieuse",
    "location": "Paris, France",
    "specialChars": "Café, naïve, résumé, piñata, 北京, 東京"
  },
  {
    "id": 2,
    "title": "北京烤鸭店",
    "description": "正宗北京烤鸭,传统工艺制作",
    "location": "北京, 中国",
    "specialChars": "中文,English,Français,Español"
  }
]

CSV de salida (codificacion UTF-8):

id,title,description,location,specialChars
1,Café & Restaurant,Un café français avec une cuisine délicieuse,Paris France,Café naïve résumé piñata 北京 東京
2,北京烤鸭店,正宗北京烤鸭传统工艺制作,北京 中国,中文EnglishFrançaisEspañol

Datos con comillas y comas

JSON de entrada:

[
  {
    "name": "Smith, John",
    "quote": "He said \"Hello, World!\" to everyone",
    "description": "A person who likes to say \"Hello\" and \"Goodbye\"",
    "address": "123 Main St, Apt 4B, New York, NY 10001"
  }
]

CSV de salida (con escape de comillas):

name,quote,description,address
"Smith, John","He said ""Hello, World!"" to everyone","A person who likes to say ""Hello"" and ""Goodbye""","123 Main St, Apt 4B, New York, NY 10001"

Ejemplos de configuracion

Formato CSV europeo

Configuracion:

  • Delimitador: punto y coma (;)
  • Codificacion: UTF-8
  • Incluir encabezados: Si
  • Escapar comillas: Si

JSON de entrada:

[
  { "name": "Müller", "city": "München", "price": "1.234,56" },
  { "name": "Schmidt", "city": "Köln", "price": "2.345,67" }
]

CSV de salida:

name;city;price
Müller;München;1.234,56
Schmidt;Köln;2.345,67

Valores separados por tabulacion (TSV)

Configuracion:

  • Delimitador: Tab
  • Codificacion: UTF-8
  • Incluir encabezados: Si
  • Escapar comillas: No

JSON de entrada:

[
  { "gene": "BRCA1", "mutation": "c.5266dupC", "significance": "Pathogenic" },
  { "gene": "TP53", "mutation": "c.742C>T", "significance": "Likely Pathogenic" }
]

TSV de salida:

gene    mutation    significance
BRCA1   c.5266dupC  Pathogenic
TP53    c.742C>T    Likely Pathogenic

Ejemplos de manejo de errores

Entrada JSON invalida

Entrada (falta una coma):

[
  {"name": "John" "age": 30}
]

Salida de error:

Invalid JSON format. Please check your input.

Datos vacios

Entrada:

[]

Salida:

(Empty CSV - no data to convert)

Tipos de datos mixtos

JSON de entrada:

[
  { "id": 1, "name": "Product A", "active": true, "price": 99.99, "tags": null },
  { "id": 2, "name": "Product B", "active": false, "price": 149.99, "tags": ["electronics"] },
  { "id": 3, "name": "Product C", "active": true, "price": null, "tags": ["gadget", "portable"] }
]

CSV de salida:

id,name,active,price,tags
1,Product A,true,99.99,
2,Product B,false,149.99,"[""electronics""]"
3,Product C,true,,"[""gadget"",""portable""]"

Ejemplos de rendimiento

Conjunto de datos grande

JSON de entrada (1000 registros):

[
  // ... 1000 user records
  {
    "id": 1,
    "name": "User 1",
    "email": "user1@example.com",
    "department": "Engineering",
    "salary": 75000,
    "hireDate": "2020-01-15"
  }
  // ... 999 more records
]

Tiempo de procesamiento: ~50 ms para 1000 registros Tamano de salida: archivo CSV de ~150 KB Uso de memoria: ~2 MB durante el procesamiento

Optimizacion de memoria

Para conjuntos de datos muy grandes, considera procesarlos por bloques:

// Process 10,000 records in chunks of 1000
const chunkSize = 1000;
const chunks = [];
for (let i = 0; i < largeDataset.length; i += chunkSize) {
  chunks.push(largeDataset.slice(i, i + chunkSize));
}

// Process each chunk
chunks.forEach((chunk, index) => {
  const csv = jsonToCSV(chunk);
  downloadCSV(csv, `data_chunk_${index + 1}.csv`);
});
¿Te resultó útil esta página?