JSON
JavaScript Object Notation (JSON) is a lightweight data-interchange format that is easy for humans to read and write. Furthermore, equally simple for machines to generate and/or parse. JSON is completely language agnostic and that's why it's the ideal interchange format.
To read more about JSON visit: json.org.
Parsing JSON
To parse a JSON string received by another application or generated within your existing application:
import json
struct Customer {
    first_name    string
    last_name     string
    hometown      string
}
fn main() {
    customers_string := '[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro" }, { "first_name": "Don", "last_name": "Nisnoni", "hometown": "Kupang" }]'
    customers := json.decode([]Customer, customers_string) or {
        eprintln('Failed to parse json')
        return
    }
    // Print the list of customers
    for customer in customers {
        println('$customer.first_name $customer.last_name: $customer.hometown')
    }
}Generating JSON
Creating a JSON string for communication or serialization is just as simple. We decode and encode in the example below:
import json
struct Customer {
    first_name    string
    last_name     string
    hometown      string
}
fn main() {
    customer_string := '[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro"}]'
    customer := json.decode([]Customer, customer_string) or {
        eprintln('Failed to parse json')
        return
    }
    encoded_json := json.encode(customer)
    println(encoded_json)
}Exercises
- Compare how you handle JSON in your favorite language and V. 
- Build an - Addressstruct that contains address information.
- Use the - Addressstruct to decode and encode a string that contains JSON format.
- Create 2 structs: - Addressand- Userwhere a user has many addresses. Now receive a string with a nested JSON like- '[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro", "addresses": [{ street_name: "Rua Princesa Isabel", city: "Rio de Janeiro", country: "Brazil" }] }]', decode and encode it.
Last updated
Was this helpful?
