3 Replies - 435 Views - Last Post: 04 June 2017 - 07:54 AM Rate Topic: -----

#1 davsank  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 31
  • Joined: 05-January 11

Reading Json File with nested fields in go

Posted 04 June 2017 - 02:21 AM

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

Quote

{"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":"[email protected]","user_name":"usertest"},"CampaignID":80099700}


and a piece of code written in go like so:
package main

import (
	"io/ioutil"
	"log"
	"strings"
	"encoding/json"
	"os"
)

func main() {

	// Create the Nested
	type userdate struct {
		user_id    int
		user_email string
		user_name  string
	}
	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
		user_data   userdate
	}

	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)

Could anyone help me with this?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Json File with nested fields in go

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,754
  • Joined: 12-December 12

Re: Reading Json File with nested fields in go

Posted 04 June 2017 - 02:36 AM

What is the issue with your code? What happens when you run it?
Was This Post Helpful? 0
  • +
  • -

#3 davsank  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 31
  • Joined: 05-January 11

Re: Reading Json File with nested fields in go

Posted 04 June 2017 - 02:39 AM

Not so mush an issue as being incomplete..
I don't know how to parse the output.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,982
  • Joined: 21-June 11

Re: Reading Json File with nested fields in go

Posted 04 June 2017 - 07:54 AM

Disclaimer: I don't really know Go or its JSON-library, so I'm mostly just educated-guessing based on your code.

View Postdavsank, on 04 June 2017 - 11:21 AM, said:

How do I include the nested fields in the structure called Message


Which nested fields? The one in user_data? You already did that by defining the user_date struct, didn't you? Or did that not work? Or did you mean without defining another struct (not sure that's possible)?

Quote

and how do I read each and every value into a variable (preferably not an array but a separate variable for each value)


Unless I misunderstood your question, that'd be something like this:

domain := m.domain
ssl := m.sll
// etc.



But I don't see why you'd do that instead of just using m.domain etc. directly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1