JSON to Array show nothing

Hi, I want to change Json data into Array, so I can save the results into text.

Here’s my JSON

{
    "data": [
        {
            "catalogId": "4f0c83ad1ef095776af6bedae1ee9d25",
            "productId": "7565909fe41a16044807acb9a9139e04",
            "keywords": null,
            "price": "28.00",
            "price_sale": "12.99",
            "percentOff": "54",
            "currency": "USD",
            "merchant": "Kohl's",
            "merchantId": "126799",
            "upc": "881634203599",
            "isbn": "",
            "sales": 0
        },
        {
            "catalogId": "5b77a23564bc048cde0d233abc8cca44",
            "productId": "8b9b89be4712a666e69852aba6d4df3a",
            "keywords": null,
            "price": "138.06",
            "price_sale": "",
            "percentOff": null,
            "currency": "USD",
            "upc": "",
            "isbn": "",
            "sales": 0
        }
    ]
}

and here is my code:

// DataPage - This is data product in json
type DataPage struct {
CatalogID string json:"catalogId"
ProductID string json:"productId"
Keywords string json:"keywords"
Price string json:"price"
PriceSale string json:"price_sale"
PercentOff string json:"percentOff"
Currency string json:"currency"
Upc string json:"upc"
Isbn string json:"isbn"
Sales string json:"sales"
}

func main() {
page := “JSON DATA ON TOP”
bytes := byte(page)

var datapage DataPage
if err := json.Unmarshal(bytes, &datapage); err != nil {
    panic(err)
}
fmt.Println(datapage)
//fmt.Fprint(w, page)

}

When I run the script, It prints nothing but { }. what did i missed?

Regards

Hey @dummy,

You forgot that data is an array in the JSON :slight_smile:

Here’s something you can do to fix it.

package main

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

type Data struct {
	CatalogID  string `json:"catalogId"`
	ProductID  string `json:"productId"`
	Keywords   string `json:"keywords"`
	Price      string `json:"price"`
	PriceSale  string `json:"price_sale"`
	PercentOff string `json:"percentOff"`
	Currency   string `json:"currency"`
	Upc        string `json:"upc"`
	Isbn       string `json:"isbn"`
	Sales      int    `json:"sales"`
}

type DataPage struct {
	D []Data `json:"data"`
}

func main() {
    // Read in the json file.
	bytes, err := ioutil.ReadFile("file.json")
	if err != nil {
		log.Fatalln(err)
	}

	var datapage DataPage
	if err := json.Unmarshal(bytes, &datapage); err != nil {
		panic(err)
	}
	fmt.Println(datapage)
}

In the above example, I put the JSON in a file called file.json to make things simpler.

Just one other thing. For the Sales field, since you used an integer in the JSON and not a string, it needed to be an switched to an int field instead of a string field to work.

Edit: You could also have used this instead of using 2 separate structures:

type DataPage struct {
	D []struct {
		CatalogID  string `json:"catalogId"`
		ProductID  string `json:"productId"`
		Keywords   string `json:"keywords"`
		Price      string `json:"price"`
		PriceSale  string `json:"price_sale"`
		PercentOff string `json:"percentOff"`
		Currency   string `json:"currency"`
		Upc        string `json:"upc"`
		Isbn       string `json:"isbn"`
		Sales      int    `json:"sales"`
	} `json:"data"`
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.