I have such json
file:
{
"types": {
"controller": {
"base": {
"speed": 1024,
"n_core": 1
}
}
},
"Server1": {
"type": {"$ref": "#/types/controller/base"},
"name": "Server1",
"is_in_json": true
},
}
It contains $ref
key. From here :
The "$ref" string value contains a URI [RFC3986], which identifies the location of the JSON value being referenced. It is an error condition if the string value does not conform to URI syntax rules. Any members other than "$ref" in a JSON Reference object SHALL be ignored.
For parsing I need to write a struct with json-tag
like this:
type Server struct {
Type ??? `json:"???"`
Name string `json:"name"`
IsInJson bool `json:"is_in_json"`
}
What should I write in place of ???
for referencing another structure?
If the key is always $ref
and that's the only key to be captured from the object, as seems to be the requirement from the documentation you cited, then you can make a type for it:
type TypeData struct {
Ref string `json:"$ref"`
}
type Server struct {
Type TypeData `json:"type"`
Name string `json:"name"`
IsInJson bool `json:"is_in_json"`
}
If that's not the case, the easiest thing is probably to assign it to a map[string]string
and then iterate through the map to process elements appropriately:
type Server struct {
Type map[string]string `json:"type"`
Name string `json:"name"`
IsInJson bool `json:"is_in_json"`
}