Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/johnpili/parse-json-data-without-struct

Parsing JSON data without concrete struct in Go
https://github.com/johnpili/parse-json-data-without-struct

go golang json parsing unmarshalling

Last synced: 23 days ago
JSON representation

Parsing JSON data without concrete struct in Go

Awesome Lists containing this project

README

        

# parse-json-data-without-struct
Parsing JSON data without concrete struct in Go

```go
package main

import (
"encoding/json"
"log"
)

var sampleJson = `{
"username": "herr.wayne",
"password": "L3akyPa5sw0rd!",
"name": "Herr Wayne",
"website": "https://example.de",
"number": 123
}`

func main() {
rawData := []byte(sampleJson)
var payload interface{} //The interface where we will save the converted JSON data.
err := json.Unmarshal(rawData, &payload) // Convert JSON data into interface{} type
if err != nil {
log.Fatal(err)
}
m := payload.(map[string]interface{}) // To use the converted data we will need to convert it into a map[string]interface{}

log.Printf("Username: %s", m["username"].(string))
log.Printf("Password: %s", m["password"].(string))
log.Printf("Name: %s", m["name"].(string))
log.Printf("Website: %s", m["website"].(string))
log.Printf("Number: %d", int(m["number"].(float64)))
}
```