Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/johnpili/parse-json-data-without-struct
- Owner: johnpili
- Created: 2022-12-04T13:06:33.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-15T20:45:26.000Z (about 1 year ago)
- Last Synced: 2024-04-17T20:18:50.372Z (7 months ago)
- Topics: go, golang, json, parsing, unmarshalling
- Language: Go
- Homepage: https://johnpili.com/how-to-parse-json-data-without-struct-in-golang/
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# parse-json-data-without-struct
Parsing JSON data without concrete struct in Go```go
package mainimport (
"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)))
}
```