User Tools

Site Tools


tanszek:oktatas:techcomm:yaml

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:techcomm:yaml [2025/11/03 20:39] – [Data Types] kneheztanszek:oktatas:techcomm:yaml [2025/11/17 20:35] (current) – [Summary] knehez
Line 157: Line 157:
   <<: *base   <<: *base
   debug: true   debug: true
 +
 +release:
 +  <<: *base
 +  debug: false
 </code> </code>
  
Line 188: Line 192:
 </code> </code>
  
-===== Educational Demo Idea ===== +
-Show the same configuration both in JSON and YAML, and ask: +
-  * Which one is easier to read? +
-  * What are the risks of using indentation as syntax? +
-  * How does the structure represent a **syntax tree**?+
  
 ===== Summary ===== ===== Summary =====
Line 198: Line 198:
   * It was created to bridge the gap between human readability and machine processing.   * It was created to bridge the gap between human readability and machine processing.
   * It plays a central role in modern **DevOps**, **configuration management**, and **data description languages**.   * It plays a central role in modern **DevOps**, **configuration management**, and **data description languages**.
 +
 +----
 +
 +===== JSON & YAML Exercises =====
 +
 +==== 1. Identify the format & fix errors ====
 +
 +**Task:**  
 +Determine whether the snippet is in JSON or YAML format. If invalid, fix it.
 +
 +<code>
 +name: ChatGPT
 +skills: ["nlp", "reasoning", "qa"]
 +version: "5.1"
 +</code>
 +
 +**Solution:**  
 +This is valid YAML.
 +
 +
 +==== 2. Convert JSON → YAML ====
 +
 +**Task:**
 +
 +<code json>
 +{
 +  "user": "John",
 +  "role": "student",
 +  "active": true,
 +  "points": 128
 +}
 +</code>
 +
 +**Solution (YAML):**
 +
 +<code yaml>
 +user: John
 +role: student
 +active: true
 +points: 128
 +</code>
 +
 +
 +==== 3. Convert YAML → JSON ====
 +
 +**Task:**
 +
 +<code yaml>
 +server:
 +  host: localhost
 +  port: 8080
 +  logging: true
 +</code>
 +
 +**Solution (JSON):**
 +
 +<code json>
 +{
 +  "server": {
 +    "host": "localhost",
 +    "port": 8080,
 +    "logging": true
 +  }
 +}
 +</code>
 +
 +
 +==== 4. Extend the YAML configuration ====
 +
 +**Task:**  
 +Add an 'admins' list (min. 2 names) and an 'ssl' setting.
 +
 +<code yaml>
 +webserver:
 +  host: 192.168.1.20
 +  port: 3000
 +</code>
 +
 +**Solution:**
 +
 +<code yaml>
 +webserver:
 +  host: 192.168.1.20
 +  port: 3000
 +  ssl: true
 +  admins:
 +    - alice
 +    - bob
 +</code>
 +
 +
 +==== 5. Valid or invalid JSON? Fix it. ====
 +
 +**Task:**
 +
 +<code json>
 +{
 +  "name": "Test app":
 +  "version": 1.0,
 +  "debug": true,
 +}
 +</code>
 +
 +**Solution:**  
 +Errors: misplaced colon, trailing comma.
 +
 +Correct version:
 +
 +<code json>
 +{
 +  "name": "Test app",
 +  "version": 1.0,
 +  "debug": true
 +}
 +</code>
 +
 +
 +==== 6. Fix the indentation errors (YAML) ====
 +
 +**Task:**
 +
 +<code yaml>
 +database:
 +    name: testdb
 +      port: 5432
 +  host: localhost
 +</code>
 +
 +**Solution:**
 +
 +<code yaml>
 +database:
 +  name: testdb
 +  port: 5432
 +  host: localhost
 +</code>
 +
 +
 +==== 7. Create the JSON structure ====
 +
 +**Task:**  
 +Create this structure in JSON:
 +
 +  * application
 +    * name: "ProdApp"
 +    * database:
 +      * host: "10.0.0.12"
 +      * port: 3306
 +      * users: ["admin", "guest"]
 +
 +**Solution:**
 +
 +<code json>
 +{
 +  "application": {
 +    "name": "ProdApp",
 +    "database": {
 +      "host": "10.0.0.12",
 +      "port": 3306,
 +      "users": ["admin", "guest"]
 +    }
 +  }
 +}
 +</code>
 +
 +
 +==== 8. Rewrite "sizes" as a list ====
 +
 +**Task:**
 +
 +<code yaml>
 +product:
 +  name: hoodie
 +  sizes: "S, M, L, XL"
 +</code>
 +
 +**Solution:**
 +
 +<code yaml>
 +product:
 +  name: hoodie
 +  sizes:
 +    - S
 +    - M
 +    - L
 +    - XL
 +</code>
 +
 +
 +==== 9. Create your own configuration ====
 +
 +**Task:**  
 +Create a configuration (YAML or JSON) including:
 +
 +  * server settings
 +  * 3 users
 +  * roles
 +  * features list (3 items)
 +
 +**Solution (YAML example):**
 +
 +<code yaml>
 +app:
 +  server:
 +    host: 0.0.0.0
 +    port: 5000
 +
 +  users:
 +    - name: anna
 +      role: admin
 +    - name: bela
 +      role: editor
 +    - name: koris
 +      role: read-only
 +
 +  features:
 +    - analytics
 +    - backup
 +    - notifications
 +</code>
 +
 +
 +==== 10. List differences between JSON and YAML ====
 +
 +**Task:**  
 +List 5 differences.
 +
 +**Solution:**
 +
 +  * YAML uses indentation instead of curly braces.
 +  * YAML supports comments (#), JSON does not.
 +  * JSON has stricter syntax; YAML is more flexible.
 +  * YAML supports advanced features (anchors, aliases).
 +  * JSON keys must be strings; YAML keys do not require quotes.
 +
 +
 +==== 11. Industrial config conversion ====
 +
 +**Task:** Convert to YAML and add `backup: true`.
 +
 +<code json>
 +{
 +  "plant": "Factory_A",
 +  "machines": [
 +    {"id": 1, "type": "CNC", "status": "online"},
 +    {"id": 2, "type": "Laser", "status": "offline"}
 +  ],
 +  "schedule": {
 +    "shift": "night",
 +    "workers": 12
 +  }
 +}
 +</code>
 +
 +**Solution:**
 +
 +<code yaml>
 +plant: Factory_A
 +machines:
 +  - id: 1
 +    type: CNC
 +    status: online
 +  - id: 2
 +    type: Laser
 +    status: offline
 +schedule:
 +  shift: night
 +  workers: 12
 +backup: true
 +</code>
 +
 +
 +==== 12. Convert string booleans to real booleans ====
 +
 +**Task:**
 +
 +<code yaml>
 +config:
 +  verbose: "true"
 +  auto_restart: "false"
 +  safe_mode: "False"
 +</code>
 +
 +**Solution:**
 +
 +<code yaml>
 +config:
 +  verbose: true
 +  auto_restart: false
 +  safe_mode: false
 +</code>
 +
  
tanszek/oktatas/techcomm/yaml.1762202340.txt.gz · Last modified: 2025/11/03 20:39 by knehez