This question already has answers here:
Golang parse a json with DYNAMIC key [duplicate]
(1 answer)
How to parse/deserialize dynamic JSON
(4 answers)
Marshal dynamic JSON field tags in Go
(1 answer)
How to Unmarshal jSON with dynamic key which can't be captured as a `json` in struct: GOlang [duplicate]
(1 answer)
Closed 4 years ago.
I'm receiving a json object that has a known-static structure inside a key that varies between 10 different values.
Consider lastname can be any in a list of 10 lastnames:
var lastnames = [...]string { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson" }
Now, this is how the json looks:
{
(lastname here):
{
"position": value,
"user_email": value
}
}
I tried to unmarshall it using the following structs, but I only get null values:
type Inside struct {
Rol string `json:"position"`
Email string `json:"user_email"`
}
type Outside struct {
Key Inside
}
...
var outside Outside
json.Unmarshal([]byte(body), &outside)
Is it possible to unmarshall this directly without creating 10 different structs? Is there possible workaround?
Related
This question already has answers here:
PowerShell iterate through json of key value pairs
(1 answer)
Powershell Selecting NoteProperty Type Objects From Object
(2 answers)
Closed 3 months ago.
Suppose I have something like this:
"config_service": {
"config_service_1": {
"service": "__service1__"
},
"config_service_2": {
"service": "__service2__"
}
},
I am able to grab out the parent json by doing
$jsonMappings.config_service
But how can I dynamically iterate over each json value in there and do something like, for each object within config_service, extract value of key named 'service'? When I try doing ForEach-Object it treats the config service 1 and 2 as one big object.
This question already has answers here:
Is it possible to partially decode and update JSON? (go)
(2 answers)
How to parse a complicated JSON with Go unmarshal?
(3 answers)
golang | unmarshalling arbitrary data
(2 answers)
How to parse JSON in golang without unmarshaling twice
(3 answers)
Decoding generic JSON objects to one of many formats
(1 answer)
Closed 9 months ago.
How to parse old json and new json simultaneously with the same struct and filed?
I am new to Go, is there any easier way to solve it? Thanks so much for any advice.
I would like to parse Json from MQTT, and I can parsed below old json successfully with below Go code, now we have new json with different formats only with test filed, and I will got the error of json: cannot unmarshal array into Go struct field Skill.SKILL.Test of type float64 when parse new json.
Old json
{
"ID" : "1",
"SKILL" : {
"name" : "test_name",
// index is the `ID` of value, the length of the list is settled
"test" : [23.1234, 15.2345, 25.2341, 27.1234, 14.2345, 16.2341,17.2341]
}
}
Go code
package main
import (
"encoding/json"
)
type MyProject struct {
Id string `json:"Id"`
Skill Skill `json:"SKILL"`
}
type Skill struct {
Name string `json:"Name"`
Test []float64 `json:"Test"`
}
func main(client mqtt.Client, msg mqtt.Message) {
myproject := MyProject{}
err := json.Unmarshal(msg.Payload(), &myproject)
test := myproject.Skill.Test
}
New json
Now we have new json as below from MQTT server:
{
"ID" : "1",
"SKILL" : {
"name" : "test_name",
// `1`,`2`,`3` is the `ID` of value,the length of the list is settled
"test" : [[1, 23.1234], [2, 13.2345], [3, 26.2341]]
}
}
This question already has answers here:
Is it possible to partially decode and update JSON? (go)
(2 answers)
How to parse a complicated JSON with Go unmarshal?
(3 answers)
golang | unmarshalling arbitrary data
(2 answers)
How to parse JSON in golang without unmarshaling twice
(3 answers)
Decoding generic JSON objects to one of many formats
(1 answer)
Closed 9 months ago.
I have a JSON in the following format:
{
"fieldA": {
"dynamicField": "string value",
"moreDynamicField": ["or", "array", "of", "strings"],
"allFieldsAreDynamic": "values in str or arr[str] only"
},
"fieldB": {
"dynamicField": "sameAsThoseInFieldA",
}
}
The outer fields are fixed, but each outer field's value is a dynamic object. Therefore, I attempted to unmarshall this data into the following Go struct:
type JsonData struct {
fieldA map[string]interface{}
fieldB map[string]interface{}
}
func main() {
var data JsonData
json.Unmarshal([]byte(`{"fieldA": {"x":"x", "y":["x","y"]}, "fieldB": {"a":"b", "c":"d"}}`), &data)
fmt.Println(data)
}
However, the above code produces empty Go array with no error: {map[] map[]}. I also tried fieldA map[string]map[string]any but the result is the same.
I didn't spot useful information on the json package documentation about this nested unmarshalling. Plus, will there be a performance drop using pure map[string]interface{} for the entire JSON data? Some benchmarking blogs and reddit tales gives contradictory conclusions...
This question already has answers here:
How do I use JSTL to output a value from a JSON string?
(1 answer)
Display JSON object using JSP/ JSTL tags in UI? [duplicate]
(1 answer)
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
I know this topic was discussed pretty often and I guess it is not a diffucult thing.
I want to use a JSON Object from my session in a JSP. The Object has the following structur:
{
"addedUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
}
],
"allUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
},
{
"city":"New York",
"name":"Peter",
"forname":"Parker",
"title":"Dr.",
"userId":3
}
]
}
Now I want to grab the Objects by name for example doing a for each on the "addedUsers" Object and grab the properties. It is important not just to iterate over the whole Object. I have to call them by name.
This question already has answers here:
Unmarshal 2 different structs in a slice
(3 answers)
Closed 4 years ago.
How would I deserialize this JSON in Go?
{
"using": [ "jmap-core", "jmap-mail" ],
"methodCalls": [
["method1", {"arg1": "arg1data", "arg2": "arg2data"}, "#1"],
["method2", {"arg1": "arg1data"}, "#2"],
["method3", {}, "#3"]
]
}
I haven't figured out how to properly get the json module to parse the methodCalls into a type. My first idea was
type MethodCall struct {
Name string
Params map[string]string
ClientId string
}
and then to use it as a list type:
type Request struct {
Using []string
MethodCalls []MethodCall
}
But this does not work. :using" is correctly parsed, but the "methocCalls" are not. Is there a way to get Go to parse this JSON into my types?
It looks like the methodCalls that you are trying to deserialize it is an array of strings instead of a struct for MethodCall.
So, Take a look at this link that I am deserializing as an array.
If you want to use the MethodCall struct you have to change the json a little bit. Take a look at this link