Json Decode Issue

Hi,

I am using the below code to decode the json which I have hardcoded in the code but when I am trying to decode that I m not getting the all values.As I have mentioned the output of that code below.So please let me know is there any other way to decode this type of json and getting all key’s values.

package main

import (
“fmt”
“encoding/json”
)

type LogEntry struct {
filename string
functionname string
level int
LineNo int
msg string
time string
}

func main() {

var c LogEntry
result := `{"filename":"caches.go","functionname":"updateCaches","level":"info","lineno":31,"msg":"updating Caches","time":"2017-01-03T18:37:34+05:30"}`
err:=json.Unmarshal([]byte(result), &c)

	if err!=nil{
		fmt.Println("here is error",err)
		
	}else{
		fmt.Println("here is no error",c)
	}

}

Output :

here is no error { 0 31 }

Hello, @Nirmal_Singh.

You should use the json notation in the struct, as follows:

type LogEntry struct {
  Filename string `json:"filename"`
  Functionname string `json:"functionname"`
  Level string `json:"level"`
  LineNo int `json:"lineno"`
  Msg string `json:"msg"`
  Time string `json:"time"`
}

Note: the field should start with a capital letter
Note2: Level is a string, not int

Edit1: Identation
Edit2: Level type is string

Structs must have exported fields for the reflect package, used by encoding/json, to read and set them.

Try this

type LogEntry struct {
        Filename string `json:"filename"`
        Functionname string `json:"functionname"`
        Level int `json:"level"`
        LineNo int `json:"lineno"`
        Msg string `json:"msg"`
        Time string `json:"time"`
}

Exactly, I was just a bit too late:

Here the working example: https://play.golang.org/p/Eo5qS0nHAH

Thank you so much!

It really worked.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.