10

I am unmarshaling a struct and I would like it to print it in a formatted manner.

My code (https://play.golang.org/p/D0KwGP6Cxa0) currently produces the following output:

main.ADIStruct{CondensedADI:[]main.CondensedADI{main.CondensedADI{Name:"Type", Value:"title"}, main.CondensedADI{Name:"Title", Value:"Ste Life_S01_E10_"}, main.CondensedADI{Name:"Title_Brief", Value:"Ste Life_S01_E10_HD"}, main.CondensedADI{Name:"Episode_Name", Value:"Cody Goes to Camp"}, main.CondensedADI{Name:"Episode_ID", Value:"10"}, main.CondensedADI{Name:"Summary_Short", Value:"Zack is excited to finally get rid of his brother when Cody leaves for math camp."}, main.CondensedADI{Name:"Rating", Value:"TV-G"}, main.CondensedADI{Name:"Run_Time", Value:"00:22:50"}, main.CondensedADI{Name:"Display_Run_Time", Value:"00:23"}, main.CondensedADI{Name:"Year", Value:"2005"}, main.CondensedADI{Name:"Closed_Captioning", Value:"Y"}, main.CondensedADI{Name:"Genre", Value:"Family"}, main.CondensedADI{Name:"Billing_ID", Value:"00000"}, main.CondensedADI{Name:"Actors_Display", Value:"Ashley Tisdale ( Maddie ), Brenda Song ( London ), Cole Sprouse ( Cody ), Dylan Sprouse ( Zack ), Kim Rhodes ( Carey ), Phill Lewis ( Moseby )"}, main.CondensedADI{Name:"Licensing_Window_Start", Value:"2019-05-15 00:00:00"}, main.CondensedADI{Name:"Licensing_Window_End", Value:"2019-10-31 00:00:00"}, main.CondensedADI{Name:"Preview_Period", Value:"0"}, main.CondensedADI{Name:"Display_As_New", Value:"7"}, main.CondensedADI{Name:"Display_As_Last_Chance", Value:"7"}, main.CondensedADI{Name:"Provider_QA_Contact", Value:"DATG.DL-VOD.Support@disney.com"}, main.CondensedADI{Name:"Suggested_Price", Value:"0.00"}, main.CondensedADI{Name:"Category", Value:"Disney Channel HD/Suite Life"}}}

I would like it to only have the name and value and have a new line after each item. Such as this:

Name:"Type", Value:"title"

Name:"Title", Value:"Ste Life_S01_E10_"

Any ideas how I could do that?

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
lakeIn231
  • 1,177
  • 3
  • 14
  • 34

2 Answers2

33

There is a function in the Go standard library taking a interface{} and producing an indented JSON output: json.MarshalIndent.

Here is an example how it could be applied to your use-case (https://play.golang.org/p/3geUEEHESSa):

s, _ := json.MarshalIndent(b, "", "\t")
fmt.Print(string(s))

While not being exactly what you expected, this produces a JSON output which is pretty readable:

{
    "CondensedADI": [
        {
            "Name": "Type",
            "Value": "title"
        },
        {
            "Name": "Title",
            "Value": "Ste Life_S01_E10_"
        },
        ...
bruceg
  • 2,433
  • 1
  • 22
  • 29
aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
  • 1
    This works wonderfully! Last question. If I want to exclude certain JSON Names, such as "Type" or "Title", is there an easy way to do that? – lakeIn231 May 21 '19 at 16:19
  • I'm not sure about this, I will dig the topic. In the meantime you should create a new question to maximize the visibility. – aymericbeaumet May 21 '19 at 16:21
  • 2
    https://stackoverflow.com/questions/56242969/how-to-exclude-certain-names-in-a-json-unmarshal-golang – lakeIn231 May 21 '19 at 16:47
  • 3
    JSON marshaling loses a lot of information about a Go struct, and doesn't work with all data types, so is often a poor choice for this sort of thing. – Jonathan Hall May 22 '19 at 06:25
  • @Flimzy Thanks, what do you recommend? I see Marcio recommended https://github.com/davecgh/go-spew – aymericbeaumet May 22 '19 at 07:09
  • 1
    What I recommend depends on your use-case. For debugging, spew is a great choice. If you're dumping data for end-user consumption, you might even need to mark it up with HTML or something. So it depends entirely. – Jonathan Hall May 22 '19 at 07:30
3

This is probably a duplicated answer for a duplicated question, but a good alternative to json.MarshalIndent() is using go-spew, that will give you more details and formatting options: https://github.com/davecgh/go-spew