Nicster15 Nicster15 - 3 years ago 168
JSON Question

Go Using Steam Rest API with JSON

I'm currently working on an educational project regarding Golang and using the Steam RESTAPI to retrieve a player's CounterStrike (CS:GO) inventory.

After spending numerous hours this is the progress I have been able to gain:

1) Calling a request to a url containing the users ID returns a JSON message with their inventory (but the JSON is a little tricky and is what I'm having trouble on).

2) I need to loop through the rgDescriptions section of the JSON.

3) I have constructed the following code (located lower down) that loops through the rgDescription, however I am unsure how to then get the value of everything inside of it.

The code:

var data map[string]interface{}
r, err := myClient.Get("" + id + "/inventory/json/730/2")
defer r.Body.Close()

result, err := ioutil.ReadAll(r.Body)

err = json.Unmarshal(result, &data)
myMap := data["rgDescriptions"]
v := reflect.ValueOf(myMap)
for _, key := range v.MapKeys() {
value := v.MapIndex(key)
fmt.Println(key, "-", value)
//fmt.Println(reflect.ValueOf(value).MapIndex(varr[0])) // My idea for making this entire thing work, however it just spits out an error instead. :(

The code above prints out something along the lines of:

1432174707_0 - map[classid:1432174707 market_hash_name:Revolver Case background_color: market_tradable_restriction:7 tags:[map[category_name:Type internal_name:CSGO_Type_WeaponCase name:Container category:Type] map[name:The Revolver Case Collection category:ItemSet category_name:Collection internal_name:set_community_10] map[internal_name:normal name:Normal category:Quality category_name:Category] map[category:Rarity color:b0c3d9 category_name:Quality internal_name:Rarity_Common name:Base Grade]] appid:730 instanceid:0 icon_url:-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFYwnfKfcG9HvN7iktaOkqD1auLTxD5SvZYgiLvFpo7xjVLh-kdrYWnzcoGLMlhpsyM-5vg market_name:Revolver Case type:Base Grade Container marketable:1 commodity:1 icon_drag_url: name:Revolver Case name_color:D2D2D2 tradable:1 descriptions:[map[type:html value: ] map[type:html value:Container Series #111 color:99ccff] map[type:html value: ] map[type:html value:Contains one of the following:] map[type:html value:R8 Revolver | Crimson Web color:4b69ff] map[type:html value:AUG | Ricochet color:4b69ff] map[type:html value:Desert Eagle | Corinthian color:4b69ff] map[value:P2000 | Imperial color:4b69ff type:html] map[type:html value:Sawed-Off | Yorick color:4b69ff] map[color:4b69ff type:html value:SCAR-20 | Outbreak] map[type:html value:PP-Bizon | Fuel Rod color:8847ff] map[type:html value:Five-SeveN | Retrobution color:8847ff] map[type:html value:Negev | Power Loader color:8847ff] map[type:html value:SG 553 | Tiger Moth color:8847ff] map[type:html value:Tec-9 | Avalanche color:8847ff] map[type:html value:XM1014 | Teclu Burner color:8847ff] map[type:html value:AK-47 | Point Disarray color:d32ce6] map[type:html value:G3SG1 | The Executioner color:d32ce6] map[type:html value:P90 | Shapewood color:d32ce6] map[type:html value:M4A4 | Royal Paladin color:eb4b4b] map[type:html value:R8 Revolver | Fade color:eb4b4b] map[value:or an Exceedingly Rare Special Item! color:ffd700 type:html] map[type:html value: ] map[color:00a000 app_data:map[limited:1] type:html value:]]]

If you'd like to see an example of the JSON I am receiving here is an example URL.

So in conclusion, I need to figure out how to loop through the contents of the value variable.

Any help would be greatly appreciated.
Thank you.

Answer Source

Below is a working example. The crux is to declare a struct type that matches your JSON.

You need to use a map type for your item since the key is not consistent.

package main

import (

type Inventory struct {
    Success      bool                    `json:"success"`
    Items        map[string]*Item        `json:"rgInventory"`
    Descriptions map[string]*Description `json:"rgDescriptions"`

type Item struct {
    ID         string `json:"id"`
    ClassID    string `json:"classid"`
    InstanceID string `json:"instanceid"`
    Amount     string `json:"amount"`
    Pos        int    `json:"pos"`

type Description struct {
    Descriptions []struct {
        Value string `json:"value"`
    } `json:"descriptions"`

func main() {
    res, err := http.Get("")
    if err != nil {

    inventory := &Inventory{}
    if err := json.NewDecoder(res.Body).Decode(&inventory); err != nil {

    for _, item := range inventory.Descriptions {
        for _, d := range item.Descriptions {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download