BBBIan
BBBIan

Reputation: 31

Golang Reading an Array of Json objects

I am using GoLang and want to read the file and be able to send each json object to a REST Endpoint.

the REST endpoint aside, I am having issues parsing the file.

  package main

    import (
        "encoding/json"
        "fmt"
        "io/ioutil"
        "bytes"
        "os"
    )

    func main() {
        type myjson struct {
            myobjects []struct {
                data map[string]string
            }
        }
        file, e := ioutil.ReadFile("dat_one_extract.json")
        if e != nil {
            fmt.Printf("File Error: [%v]\n", e)
            os.Exit(1)
        }

        dec := json.NewDecoder(bytes.NewReader(file))
        var d myjson
        dec.Decode(&d)


    }

My Json file looks like this:

[{
     "NAME1": "Y",
    "NAME2": 1729.0,
    "NAME3": "Y",
    "NAME4": [
        {
             "Contact Zip": "33619",
            "Date of Contact": "01/21/2015"
        }
    ],
     "NAME6": "123456789",
    "NAME7": "Walmart",
    "NAME8": [
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        }
    ],
    "NAME11": "XXXXXXX",
    "NAME12": "11/21/2014 14:01:47",
    "NAME13": "11/15/2014",
    "NAME14": "11/16/1992"
},{
    "NAME1": "Y",
    "NAME2": 1729.0,
    "NAME3": "Y",
    "NAME4": [
        {
             "Contact Zip": "33619",
            "Date of Contact": "01/21/2015"
        }
    ],
     "NAME6": "123456789",
    "NAME7": "Walmart",
    "NAME8": [
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        }
    ],
    "NAME11": "XXXXXXX",
    "NAME12": "11/21/2014 14:01:47",
    "NAME13": "11/15/2014",
    "NAME14": "11/16/1992"
}]

What am I missing to access each array element and pass it to the end point? I don't want to process the elements, just send the json object to the endpoint 1 at time.

Thank you in advance and apologies for being a golang noob!

Upvotes: 1

Views: 4481

Answers (1)

maerics
maerics

Reputation: 156434

// Parse the JSON.
var objs interface{}
json.Unmarshal([]byte(jsonStr), &objs) // Or use json.Decoder.Decode(...)

// Ensure that it is an array of objects.
objArr, ok := objs.([]interface{})
if !ok {
    log.Fatal("expected an array of objects")
}

// Handle each object as a map[string]interface{}.
for i, obj := range objArr {
    obj, ok := obj.(map[string]interface{})
    if !ok {
        log.Fatalf("expected type map[string]interface{}, got %s", reflect.TypeOf(objArr[i]))
    }
    fmt.Printf("i=%d, o=%T\n", i, obj) // Do something with the object...
}

Upvotes: 1

Related Questions