0
type Apis struct {
    Items []struct {
        ID                    string `json:"id"`
        Name                  string `json:"name"`
        Description           string `json:"description"`
        CreatedDate           int    `json:"createdDate"`
        APIKeySource          string `json:"apiKeySource"`
        EndpointConfiguration struct {
            Types []string `json:"types"`
        } `json:"endpointConfiguration"`
    } `json:"items"`
}

This the struct I have defined to store the APIs i get in json format. How do I get a specific API by its name and then get it's ID. For example lets say, apiname == Shopping and i want Shopping API's ID assigned to id variable.

ps : I'm new to golang and a well explained answer will be very much appreciated. Thanks guys

1 Answers1

0

In your case Items is slice of custom structs, so you have to perform search over loop, like this:

package main

import (
    "encoding/json"
    "fmt"
)

type Apis struct {
    Items []struct {
        ID                    string `json:"id"`
        Name                  string `json:"name"`
        Description           string `json:"description"`
        CreatedDate           int    `json:"createdDate"`
        APIKeySource          string `json:"apiKeySource"`
        EndpointConfiguration struct {
            Types []string `json:"types"`
        } `json:"endpointConfiguration"`
    } `json:"items"`
}

func main() {
    // Some JSON example:
    jsonStr := `{"items": [{"id":"1","name":"foo"},{"id":"2","name":"bar"}]}`

    // Unmarshal from JSON into Apis struct.
    apis := Apis{}
    err := json.Unmarshal([]byte(jsonStr), &apis)
    if err != nil {
        // error handling
    }

    // Actual search:
    nameToFind := "bar"
    for _, item := range apis.Items {
        if item.Name == nameToFind {
            fmt.Printf("found: %+v", item.ID)
            break
        }
    }
}

It would be better to have map of custom structs instead of slice, so you could to do something like this:

package main

import (
    "encoding/json"
    "fmt"
)

type Apis struct {
    Items map[string]struct {
        ID                    string `json:"id"`
        Name                  string `json:"name"`
        Description           string `json:"description"`
        CreatedDate           int    `json:"createdDate"`
        APIKeySource          string `json:"apiKeySource"`
        EndpointConfiguration struct {
            Types []string `json:"types"`
        } `json:"endpointConfiguration"`
    } `json:"items"`
}

func main() {
    // Some JSON example:
    jsonStr := `{"items": {"foo":{"id":"1","name":"foo"},"bar":{"id":"2","name":"bar"}}}`

    // Unmarshal from JSON into Apis struct.
    apis := Apis{}
    err := json.Unmarshal([]byte(jsonStr), &apis)
    if err != nil {
        // error handling
    }

    // Actual search:
    nameToFind := "bar"
    item, found := apis.Items[nameToFind]
    if !found {
        fmt.Printf("item not found")
    }
    fmt.Printf("found: %+v", item)
}

IMPORTANT: with slice you complexity of algorithm will be O(n) with map - O(1) which is way better (it's best what possible).

cn007b
  • 16,596
  • 7
  • 59
  • 74