ABC
ABC

Reputation: 2158

Dynamic JSON struct, API result golang

I have to make two HTTP API calls in GoLang, the first API call returns this json response:

{
  "status": 200,
  "msg": "OK",
  "result": {
    "id": "24",
    "folderid": "4248"
  }
}

My json struct for first response is setup like this:

type One struct {
    Status int    `json:"status"`
    Msg    string `json:"msg"`
    Result struct {
        ID       string `json:"id"`
        Folderid string `json:"folderid"`
    } `json:"result"`
}

The second call is where the problem is. As you can see the first API call returns a result -> id. This ID should be the name for the beginning of my second struct, but I can't seem how to make it dynamic or put a result as my structure name. This ID (24) will always change based on the first API call. I have no way currently to parse the second call's JSON and setup my struct. On the second API call I want to access the remoteurl/status.

Second call result (I can not parse):

{
  "status": 200,
  "msg": "OK",
  "result": {
    24: ** THIS IS DYNAMIC** {
      "id": 24,
      "remoteurl": "http://proof.ovh.net/files/100Mio.dat",
      "status": "new",
      "bytes_loaded": null,
      "bytes_total": null,
      "folderid": "4248",
      "added": "2015-02-21 09:20:26",
      "last_update": "2015-02-21 09:20:26",
      "extid": false,
      "url": false
    }
  }
}

Does anyone know how to setup my struct or go about this. I am a new programmer and go and have worked on this for 4 days. And decided to ask for some help, since I am in school and have normal homework.

Found that using JSON-to-GO helped solve future problems, will create the structs and other necessities based off a JSON content.

Upvotes: 0

Views: 1859

Answers (1)

reticentroot
reticentroot

Reputation: 3682

{
  "status": 200,
  "msg": "OK",
  "result": {
    24:  {
      "id": 24,
      "remoteurl": "http://proof.ovh.net/files/100Mio.dat",
      "status": "new",
      "bytes_loaded": null,
      "bytes_total": null,
      "folderid": "4248",
      "added": "2015-02-21 09:20:26",
      "last_update": "2015-02-21 09:20:26",
      "extid": false,
      "url": false
    }
  }
}

Is not value JSON. You MUST mean the JSON i posted below, if you want to check yourself, copy your version of the JSON into any JSON validator;

https://jsonlint.com/

https://jsoneditoronline.org/

https://jsonformatter.curiousconcept.com/

Also view the thread linked below.. if the API truly is returning what you claim it returns then the API has a bug in it

Why JSON allows only string to be a key?

{
  "status": 200,
  "msg": "OK",
  "result": {
    "24":  {
      "id": 24,
      "remoteurl": "http://proof.ovh.net/files/100Mio.dat",
      "status": "new",
      "bytes_loaded": null,
      "bytes_total": null,
      "folderid": "4248",
      "added": "2015-02-21 09:20:26",
      "last_update": "2015-02-21 09:20:26",
      "extid": false,
      "url": false
    }
  }
}

Here is some example code that uses a map to a struct what solves the dynamic response of the second response

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

var res1 = `{
  "status": 200,
  "msg": "OK",
  "result": {
    "id": "24",
    "folderid": "4248"
  }
}`

var res2 = `{
  "status": 200,
  "msg": "OK",
  "result": {
    "24":  {
      "id": 24,
      "remoteurl": "http://proof.ovh.net/files/100Mio.dat",
      "status": "new",
      "bytes_loaded": null,
      "bytes_total": null,
      "folderid": "4248",
      "added": "2015-02-21 09:20:26",
      "last_update": "2015-02-21 09:20:26",
      "extid": false,
      "url": false
    }
  }
}
`

type One struct {
    Status int    `json:"status"`
    Msg    string `json:"msg"`
    Result struct {
        ID       string `json:"id"`
        Folderid string `json:"folderid"`
    } `json:"result"`
}

type Two struct {
    Status int                  `json:"status"`
    Msg    string               `json:"msg"`
    Result map[string]innerData `json:"result"`
}

type innerData struct {
    ID          int         `json:"id"`
    Remoteurl   string      `json:"remoteurl"`
    Status      string      `json:"status"`
    BytesLoaded interface{} `json:"bytes_loaded"`
    BytesTotal  interface{} `json:"bytes_total"`
    Folderid    string      `json:"folderid"`
    Added       string      `json:"added"`
    LastUpdate  string      `json:"last_update"`
    Extid       bool        `json:"extid"`
    URL         bool        `json:"url"`
}

func main() {
    var one One
    err := json.Unmarshal([]byte(res1), &one)
    if err != nil {
        log.Fatal(err)
    }

    var two Two
    err = json.Unmarshal([]byte(res2), &two)
    if err != nil {
        log.Fatal(err)
    }

    //pretty print both strutures
    b, _ := json.MarshalIndent(one, "", " ")
    fmt.Printf("%s \n\n", b)
    b, _ = json.MarshalIndent(two, "", " ")
    fmt.Printf("%s \n\n", b)

    // access data from two with id from one
    if dat, ok := two.Result[one.Result.ID]; ok {
        b, _ = json.MarshalIndent(dat, "", " ")
        fmt.Printf("inner data\n%s\n", b)
    }

}

Upvotes: 1

Related Questions