jwesonga
jwesonga

Reputation: 4383

Represent JSON response as struct

I'm doing a call to Google Translate API and would like to represent the response as a struct. The JSON response is:

{
 "data": {
  "translations": [
   {
    "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
    "detectedSourceLanguage": "en"
   }
  ]
 }
}

I've tried to come up with a struct:

type Translations struct{
  TranslatedText string
  SourceLanguage string
}

type Translation struct{
  Data string
  Value *[]Translations
}

or:

type Translations struct{
  TranslatedText string
  SourceLanguage string
}

type Translation struct{
  Data string
  Value Translations
}

Which is the correct way to do it?

Upvotes: 2

Views: 336

Answers (2)

Daniel
Daniel

Reputation: 38771

Here's a working example that consolidates the two struct definitions into one.

http://play.golang.org/p/nI0Qet6R78

package main

import (
    "fmt"
    "encoding/json"
    )

type Translation struct{
    Data struct {
        Translations []struct {
            TranslatedText string
            DetectedSourceLanguage string
        }
    }
}

func main() {

    source := []byte(`
    {
     "data": {
      "translations": [
       {
        "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
        "detectedSourceLanguage": "en"
       }
      ]
     }
    }
    `)

    var j Translation
    err := json.Unmarshal(source, &j)
    if err != nil {
        panic(err)
    }
    for _,t := range j.Data.Translations {
        fmt.Printf("----\n")
        fmt.Printf("translatedText: %s\n", t.TranslatedText)
        fmt.Printf("detectedSourceLanguage: %s\n", t.DetectedSourceLanguage)
    }
}

Upvotes: 4

matthias krull
matthias krull

Reputation: 4429

You can break your data into three parts. A single translation which is contained in a translations array. Your major structure is data.

I find it most easy to start at the most inner structure and define that and then work my way to the outer structs.

The representation of those can be defined like this:

type translation struct {
    TranslatedText string
    SourceLanguage string `json:"detectedSourceLanguage"`
}

type translations struct {
    Translations []translation
}

type data struct {
    Data translations
}

If the data structure is not too complex you can put it all together like this:

type data struct {
    Data struct {
        Translations []struct {
            TranslatedText string
            SourceLanguage string `json:"detectedSourceLanguage"`
        }
    }
}

In a full working example:

package main

import (
    "encoding/json"
    "fmt"
)

var json_s string = `{
  "data": {
    "translations": [
      {
        "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}`

type data struct {
    Data struct {
        Translations []struct {
            TranslatedText string
            SourceLanguage string `json:"detectedSourceLanguage"`
        }
    }
}

func main() {
    var translation data
    err := json.Unmarshal([]byte(json_s), &translation)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v", translation)
}

Upvotes: 0

Related Questions