JSON
{
"id" : "12387",
"inv" :[
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
},
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
}
],
"charges" : {
"fee" : 24 ,
"bkg" : 7676
}
}
My JSON structure is like that shown above. I need to take amnt in inv-addCharges
in an array. If it has ten elements in the array, I need to get that in an array containing the particular amnt
alone in such a way like
[{"amnt" : 34 } ,{"amnt" : 34} .... so on ]
Things I tried:
var j map[string]interface{}
err := json.Unmarshal([]byte(ticket), &j)
if err != nil {
panic(err)
}
// Pull out the parents object
bytInv := j["inv"].([]interface{})
// // Print out mother and father
fmt.Println(bytInv)
Output
[map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]] map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]]]
After this, I was not able to proceed any further.
Note: I don't want to use structs because I have many JSON structures for this. And that's my requirement given.