w

Exemples

Exemples pratiques montrant differents cas d'usage du convertisseur JSON vers CSV.

Exemples de base

Conversion simple de tableau

JSON d'entree:

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

CSV de sortie:

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

Conversion d'un objet unique

JSON d'entree:

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

CSV de sortie:

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

Exemples concrets

Donnees produit e-commerce

JSON d'entree (catalogue produit):

[
  {
    "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 sortie (aplati):

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

Donnees de reponse API

JSON d'entree (API de gestion des utilisateurs):

{
  "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 sortie (tableau users uniquement):

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

Exemples avances

Donnees financieres

JSON d'entree (donnees boursieres):

[
  {
    "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 sortie:

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}"

Donnees d'enquete

JSON d'entree (enquete de satisfaction client):

[
  {
    "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 sortie:

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

Gestion des caracteres speciaux

Donnees internationales

JSON d'entree (contenu 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 sortie (encodage 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

Donnees avec guillemets et virgules

JSON d'entree:

[
  {
    "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 sortie (avec echappement des guillemets):

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"

Exemples de configuration

Format CSV europeen

Parametres:

  • Delimiteur : point-virgule (;)
  • Encodage : UTF-8
  • Inclure les en-tetes : Oui
  • Echappement des guillemets : Oui

JSON d'entree:

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

CSV de sortie:

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

Valeurs separees par tabulation (TSV)

Parametres:

  • Delimiteur : tabulation
  • Encodage : UTF-8
  • Inclure les en-tetes : Oui
  • Echappement des guillemets : Non

JSON d'entree:

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

TSV de sortie:

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

Exemples de gestion des erreurs

Entree JSON invalide

Entree (virgule manquante):

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

Sortie d'erreur:

Invalid JSON format. Please check your input.

Donnees vides

Entree:

[]

Sortie:

(Empty CSV - no data to convert)

Types de donnees mixtes

JSON d'entree:

[
  { "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 sortie:

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

Exemples de performance

Grand jeu de donnees

JSON d'entree (1000 enregistrements):

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

Temps de traitement : ~50 ms pour 1000 enregistrements Taille de sortie : fichier CSV d'environ 150 Ko Utilisation memoire : ~2 Mo pendant le traitement

Optimisation memoire

Pour les tres grands jeux de donnees, pensez a traiter par lots :

// 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`);
});
Cette page vous a-t-elle été utile ?