Dynamic Json Construction
ahambrahma opened this issue · 2 comments
I am working on a project where we are required to build dynamic json objects from a map of dotted json paths and their values. We should be able to convert the constructed json object into an already defined struct.
For ex:
Given an input Map:
"success": true "data.name": "shubham" "data.age": 21 "data.height": 172
We should be able to generate this json object
{ "success": true, "data" : { "name" : "shubham", "age": 21, "height": 172 } }
and convert it into UserResponse
type UserResponse struct {
Success bool json:"success"
Data User json:"data"
}
type User struct {
Name string json:"name"
Age int json:"age"
Height int json:"height"
}
Can someone help with how we can achieve this using jsonparser ?
Hi Guys,
Any update on this?
Hi @shubham140395,
I didn't used jsonparser, but one solution I have used is to unmarshal it to UserResponse struct like this.
var userResponse UserResponse
err := json.Unmarshal([]byte(jsondata), &userResponse)
Hope this helps to you.