0

I have the following json object that I am trying to represent with Go as a type JsonObject struct and pass it back in its original json so that I can return the json as an api endpoint. Any advice/example?

[{
    "time": 173000,
    "id": "VLSuEE5m1kmIhgE7ZhHDFe",
    "height": "",
    "DATASTRUCTURE": {

    },
    "language": "en",
    "size": 0,
    "url": "http://www.gstatic.com/play.m3u8",
    "type": "vid",
    "definitionid": "h264",
    "reference": "PAN-EN",
    "content": "This is some content",
    "revisiondate": "2017-11-29T00:00:00",
    "data": {

    },
    "id": "BBBB3424-153E-49DE-4786-013B6611BBBB",
    "thumbs": {
        "32": "https://www.gstatic.com/images?q=tbn:ANd9GcRj",
        "64": "https://www.gstatic.com/images?q=tbn:DPd3GcRj"
    },
    "title": "Cafeteria",
    "hash": "BBBB5d39bea20edf76c94133be61BBBB"
}]
1
  • first of all your json is not valid, There are two id in the same object. and according to the data you have type JsonObject map[string]interface{} might suit you. Commented Jul 9, 2018 at 12:25

1 Answer 1

1

You can use https://mholt.github.io/json-to-go/ to generate struct for the given json schema. For example, json given in the question can be represented like:

type AutoGenerated []struct {
Time          int    `json:"time"`
ID            string `json:"id"`
Height        string `json:"height"`
DATASTRUCTURE struct {
} `json:"DATASTRUCTURE"`
Language     string `json:"language"`
Size         int    `json:"size"`
URL          string `json:"url"`
Type         string `json:"type"`
Definitionid string `json:"definitionid"`
Reference    string `json:"reference"`
Content      string `json:"content"`
Revisiondate string `json:"revisiondate"`
Data         struct {
} `json:"data"`
Thumbs struct {
    Num32 string `json:"32"`
    Num64 string `json:"64"`
} `json:"thumbs"`
Title string `json:"title"`
Hash  string `json:"hash"`}

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

That is exactly what I used. What I am trying to do next is pass it in a func as such: profile := JsonObject{173000,"VLSuEE5m1kmIhgE7ZhHDFe","","en",0,"http://video/playlist.m3u8","m3u8","h264","PAN-EN","","2016-09-29T00:00:00"} body, _ := json.Marshal(profile) . I am not sure how to handle the DATASTRUCTURE portion.
The DATASTRUCTURE part is an empty object. No need set anything in the Go code.
but I have to pass someting otherwise the JsonObject complains that I am not passing the same amount of fields. I can't just pass "" for it.
@FutureDroid You can skip DATASTRUCTURE field in the Go code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.