Decode JSON

Examples Filter
Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types.
package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type response1 struct {
    Page   int
    Fruits []string
}

type response2 struct {
    Page   int      `json:"page"`
    Fruits []string `json:"fruits"`
}

func main() {

    bolB, _ := json.Marshal(true)
    fmt.Println(string(bolB))

    intB, _ := json.Marshal(1)
    fmt.Println(string(intB))

    fltB, _ := json.Marshal(2.34)
    fmt.Println(string(fltB))

    strB, _ := json.Marshal("gopher")
    fmt.Println(string(strB))

    slcD := []string{"apple", "peach", "pear"}
    slcB, _ := json.Marshal(slcD)
    fmt.Println(string(slcB))

    mapD := map[string]int{"apple": 5, "lettuce": 7}
    mapB, _ := json.Marshal(mapD)
    fmt.Println(string(mapB))

    res1D := &response1{
        Page:   1,
        Fruits: []string{"apple", "peach", "pear"}}
    res1B, _ := json.Marshal(res1D)
    fmt.Println(string(res1B))

    res2D := &response2{
        Page:   1,
        Fruits: []string{"apple", "peach", "pear"}}
    res2B, _ := json.Marshal(res2D)
    fmt.Println(string(res2B))

    byt := []byte(`{"num":6.13,"strs":["a","b"]}`)

    var dat map[string]interface{}

    if err := json.Unmarshal(byt, &dat); err != nil {
        panic(err)
    }
    fmt.Println(dat)

    num := dat["num"].(float64)
    fmt.Println(num)

    strs := dat["strs"].([]interface{})
    str1 := strs[0].(string)
    fmt.Println(str1)

    str := `{"page": 1, "fruits": ["apple", "peach"]}`
    res := response2{}
    json.Unmarshal([]byte(str), &res)
    fmt.Println(res)
    fmt.Println(res.Fruits[0])

    enc := json.NewEncoder(os.Stdout)
    d := map[string]int{"apple": 5, "lettuce": 7}
    enc.Encode(d)
}
Last Run  :
true
1
2.34
"gopher"
["apple","peach","pear"]
{"apple":5,"lettuce":7}
{"Page":1,"Fruits":["apple","peach","pear"]}
{"page":1,"fruits":["apple","peach","pear"]}
map[num:6.13 strs:[a b]]
6.13
a
{1 [apple peach]}
apple
{"apple":5,"lettuce":7}
// JSON is very popular nowadays, that's why JSON support is built in.
// The first argument of the json.decode function is the type to decode to. 
// The second argument is the JSON string.
// V generates code for JSON encoding and decoding. No runtime reflection is used.
// This results in much better performance.

import json

struct Foo {
	bar int
}

struct User {
	name string
	age  int

	// Use the `skip` attribute to skip certain fields
	foo Foo [skip]  

	// If the field name is different in JSON, it can be specified
	last_name string [json:lastName]  
}

data := '{ "name": "Frodo", "lastName": "Baggins", "age": 25 }'
user := json.decode(User, data) or {
	eprintln('Failed to decode json')
	return
}
println(user.name)
println(user.last_name)
println(user.age)
Last Run  :
V panic: array.get: index out of range (i == -1, a.len == 0)
                                               | 0x55c616e21dd2 | /code/root/v/v(+0x4dd2) 
                                               | 0x55c616e20a7d | /code/root/v/v(+0x3a7d) 
                                               | 0x55c616e8d883 | /code/root/v/v(+0x70883) 
                                               | 0x55c616e86754 | /code/root/v/v(+0x69754) 
                                               | 0x55c616e9d910 | /code/root/v/v(+0x80910) 
                                               | 0x55c616e9cfa1 | /code/root/v/v(+0x7ffa1) 
                                               | 0x55c616e9c853 | /code/root/v/v(+0x7f853) 
                                               | 0x55c616e820b7 | /code/root/v/v(+0x650b7) 
                                               | 0x55c616e9b7c3 | /code/root/v/v(+0x7e7c3) 
                                               | 0x55c616e989f5 | /code/root/v/v(+0x7b9f5) 
                                               | 0x55c616e9807a | /code/root/v/v(+0x7b07a) 
                                               | 0x55c616ead4b0 | /code/root/v/v(+0x904b0) 
                                               | 0x55c616e7c73e | /code/root/v/v(+0x5f73e) 
                                               | 0x55c616e7a72b | /code/root/v/v(+0x5d72b) 
                                               | 0x55c616e759ee | /code/root/v/v(+0x589ee) 
                                               | 0x55c616eb824c | /code/root/v/v(+0x9b24c) 
                                               | 0x55c616eb84e1 | /code/root/v/v(+0x9b4e1) 
                                               | 0x55c616eda430 | /code/root/v/v(+0xbd430) 
                                               | 0x55c616edfd02 | /code/root/v/v(+0xc2d02) 
                                               | 0x55c616eeae44 | /code/root/v/v(+0xcde44) 
                                               | 0x7fd08f36fb97 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) 
                                               | 0x55c616e1fbba | /code/root/v/v(+0x2bba)