Link to home
Start Free TrialLog in
Avatar of David Sankovsky
David SankovskyFlag for Israel

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

{"domain":"jsontest.campaigns.ninja","ssl":1,"server_key":"","certificate":"","bundle":"","php_ver":"5.6","wp":1,"lang":"english","start_date":"2017-02-02","end_date":"2017-03-02","description":"This is a test","user_data":{"user_id":"74","user_email":"usertest@gmail.com","user_name":"usertest"},"CampaignID":80099700}

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)
}

Open in new window


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
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David Sankovsky

ASKER

Script worked like magic