Exemplos
Exemplos praticos que demonstram varios casos de uso do conversor de JSON para CSV.
Exemplos basicos
Conversao simples de array
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 saida:
name,age,city
Alice,28,New York
Bob,32,Los Angeles
Charlie,25,Chicago
Conversao de objeto unico
JSON de entrada:
{
"product": "Laptop",
"price": 1299.99,
"category": "Electronics",
"inStock": true,
"tags": ["portable", "gaming", "work"]
}
CSV de saida:
product,price,category,inStock,tags
Laptop,1299.99,Electronics,true,"[""portable"",""gaming"",""work""]"
Exemplos do mundo real
Dados de produtos de e-commerce
JSON de entrada (catalogo de produtos):
[
{
"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 saida (achatado):
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
Dados de resposta de API
JSON de entrada (API de gestao de utilizadores):
{
"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 saida (apenas o array 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
Exemplos avancados
Dados financeiros
JSON de entrada (dados do mercado de acoes):
[
{
"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 saida:
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}"
Dados de inquerito
JSON de entrada (inquerito de satisfacao do 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 saida:
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
Tratamento de caracteres especiais
Dados internacionais
JSON de entrada (conteudo 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 saida (codificacao 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
Dados com aspas e virgulas
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 saida (com escape de aspas):
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"
Exemplos de configuracao
Formato CSV europeu
Definicoes:
- Delimitador: ponto e virgula (;)
- Codificacao: UTF-8
- Incluir cabecalhos: Sim
- Escapar aspas: Sim
JSON de entrada:
[
{ "name": "Müller", "city": "München", "price": "1.234,56" },
{ "name": "Schmidt", "city": "Köln", "price": "2.345,67" }
]
CSV de saida:
name;city;price
Müller;München;1.234,56
Schmidt;Köln;2.345,67
Valores separados por tabulacao (TSV)
Definicoes:
- Delimitador: Tab
- Codificacao: UTF-8
- Incluir cabecalhos: Sim
- Escapar aspas: Nao
JSON de entrada:
[
{ "gene": "BRCA1", "mutation": "c.5266dupC", "significance": "Pathogenic" },
{ "gene": "TP53", "mutation": "c.742C>T", "significance": "Likely Pathogenic" }
]
TSV de saida:
gene mutation significance
BRCA1 c.5266dupC Pathogenic
TP53 c.742C>T Likely Pathogenic
Exemplos de tratamento de erros
Entrada JSON invalida
Entrada (virgula em falta):
[
{"name": "John" "age": 30}
]
Saida de erro:
Invalid JSON format. Please check your input.
Dados vazios
Entrada:
[]
Saida:
(Empty CSV - no data to convert)
Tipos de dados mistos
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 saida:
id,name,active,price,tags
1,Product A,true,99.99,
2,Product B,false,149.99,"[""electronics""]"
3,Product C,true,,"[""gadget"",""portable""]"
Exemplos de desempenho
Grande conjunto de dados
JSON de entrada (1000 registos):
[
// ... 1000 user records
{
"id": 1,
"name": "User 1",
"email": "user1@example.com",
"department": "Engineering",
"salary": 75000,
"hireDate": "2020-01-15"
}
// ... 999 more records
]
Tempo de processamento: ~50ms para 1000 registos Tamanho da saida: ficheiro CSV de ~150KB Uso de memoria: ~2MB durante o processamento
Otimizacao de memoria
Para conjuntos de dados muito grandes, considere processar em blocos:
// 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`);
});