1

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!

BBBIan
  • 31
  • 1
  • 2
  • 2
    possible duplicate of [My structures are not marshalling into json](http://stackoverflow.com/questions/15452004/my-structures-are-not-marshalling-into-json) – Ainar-G Apr 06 '15 at 00:32
  • 1
    **Never** ignore errors (e.g. from `Decode`). – Dave C Apr 06 '15 at 03:12

1 Answers1

1
// 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...
}
maerics
  • 151,642
  • 46
  • 269
  • 291
  • Thank you for the answer. I can read the file and its in the Array and map. I have questions/challenge with the solution: 1. The file is an extract and has 15,000 + Objs; hence, the file is about 50 meg. I dont want to read the whole file into the memory before doing anything with it. So, how would I read it one obj at a time? Thank you Again. – BBBIan Apr 06 '15 at 21:22
  • @BBBIan: this answer might help - http://stackoverflow.com/questions/29421470/how-to-parse-an-infinite-json-array-from-stdin-in-go/29425099#29425099 – maerics Apr 06 '15 at 21:27
  • Thanks Life saving – Prince Hamza Nov 24 '21 at 16:16