David Sankovsky
asked on
Reading Json File with nested fields in go
Hi guys,
I'm very new to Go and I'm afraif the guides regarding JSON aren't very clear.
I have the following JSON output (saved as a .json file in a certain path) the content of which is
and the following Piece of code written in Go:
Where b should be the path to the json file.
but as you can see, the JSON itswelf has nested fields...
My question is,
and I'm sorry if it's a long one but I really tried looking it up just can't get the concept...
How do I include the nested fields in the structure called Message and how do I read each and every value into a variable (preferably not an array but a separate variable for each value)
I'm very new to Go and I'm afraif the guides regarding JSON aren't very clear.
I have the following JSON output (saved as a .json file in a certain path) the content of which is
{"domain":"jsontest.campaigns.ninja" ,"ssl":1," server_key ":"","cert ificate":" ","bundle" :"","php_v er":"5.6", "wp":1,"la ng":"engli sh","start _date":"20 17-02-02", "end_date" :"2017-03- 02","descr iption":"T his is a test","user_data":{"user_i d":"74","u ser_email" :"usertest @gmail.com ","user_na me":"usert est"},"Cam paignID":8 0099700}
and the following Piece of code written in Go:
package main
import (
"io/ioutil"
"log"
"strings"
"encoding/json"
"os"
)
func main() {
type Message struct {
domain string
ssl int
server_key string
certificate string
bundle string
php_ver string
wp int
lang string
start_date string
end_date string
Description string
}
var m Message
err := json.Unmarshal(b, &m)
}
Where b should be the path to the json file.
but as you can see, the JSON itswelf has nested fields...
My question is,
and I'm sorry if it's a long one but I really tried looking it up just can't get the concept...
How do I include the nested fields in the structure called Message and how do I read each and every value into a variable (preferably not an array but a separate variable for each value)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Script worked like magic