I have JSON file that looks like this:
{
"env": {
"production": {
"test": {
"text": "hello"
},
"url": {
"str": "url1"
}
},
"staging": {
"test": {
"text": "hel1lo"
},
"url": {
"str": "url31"
}
}
}
}
Is there a way to import this file and get into nested struct format just for staging and its inner fields in correct order?
Use this code to parse the staging data to a Go value:
type env struct {
Test struct {
Text string
}
URL struct {
Str string
}
}
var v struct {
Env struct {
Staging env
}
}
err := json.Unmarshal(data, &v)
if err != nil {
// handle error
}
staging := v.Env.Staging
JSON object fields are unordered. The Go standard library does not provide a way to get object fields in source order.
While, I'm not familiar with Go and its JSON facilities JSON objects are specified as unordered. If you require your input be in a particular order you'll need to use an array.
From json.org:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).