May 28, 2023

Decode a URL string encoded compliant with RFC 3986 to json with golang

You could use url.QueryUnEscape to decode the string and then use json.Marshal to convert to a json

package main

import (
    "net/url"
    "log"
    "fmt"
    "encoding/json"
)

type User struct {
    Name string `json:"name"`
    Age int `json:"age"`
}

func main() {
    s := "%7B%22name%22%3A%22user1%22%2C%22age%22%3A30%7D" // URL encoded value

    in, err := url.QueryUnescape(s)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Printf("query escaped %s \n", in)
    var u User
    // unmarshalling
    json.Unmarshal([]byte(in),&u)
    fmt.Printf("username : %s,  age %d \n", u.Name, u.Age)

    //marshalling
    bytes , err := json.Marshal(in)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println(string(bytes))
}

Leave a Reply