Golang parse JSON array into data structure

Kiril

I am trying to parse a file which contains JSON data:

[
  {"a" : "1"},
  {"b" : "2"},
  {"c" : "3"}
]

Since this is a JSON array with dynamic keys, I thought I could use:

type data map[string]string

However, I cannot parse the file using a map:

c, _ := ioutil.ReadFile("c")
dec := json.NewDecoder(bytes.NewReader(c))
var d data
dec.Decode(&d)


json: cannot unmarshal array into Go value of type main.data

What would be the most simple way to parse a file containing a JSON data is an array (only string to string types) into a Go struct?

EDIT: To further elaborate on the accepted answer -- it's true that my JSON is an array of maps. To make my code work, the file should contain:

{
  "a":"1",
  "b":"2",
  "c":"3"
}

Then it can be read into a map[string]string

Steve P.

It's because your json is actually an array of maps, but you're trying to unmarshall into just a map. Try using the following:

type YourJson struct {
    YourSample []struct {
        data map[string]string
    } 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Golang: structure to generate/parse both XML and JSON

From Dev

Golang structure for json to allow a value to optionally be an array

From Dev

How to parse nested JSON data structure

From Dev

How to parse nested JSON data structure

From Dev

Parse JS array in JSON string as Golang map

From Dev

How to parse a json array with the same structure but different names

From Dev

How to parse JSON data without key names into a Java class structure?

From Dev

how to parse data from json Array

From Dev

Making minimal modification to JSON data without a structure in golang

From Dev

Golang - unable to parse JSON

From Dev

How to parse JSON arrays with two different data types into a struct in Golang

From Dev

How to parse JSON data without define array in JSON in Android Studio

From Dev

How to parse a structure array as a parameter?

From Dev

How to parse this Json structure in Java

From Dev

Unable to parse this json file in golang

From Dev

How can I deserialize a JSON array into a native .net data structure?

From Dev

"no implicit conversion of Array into String" when trying to parse JSON data

From Dev

Highcharts csv data loading and parse into separate array and json

From Dev

How can I parse json data as array in javascript?

From Dev

How to parse nested Json Object inside Array Data in Ios

From Dev

Parse data from uint8array to string to json

From Dev

Is this JSON structure an array or an object?

From Dev

JSON and array structure typeahead

From Dev

Is the array structure redundant in json?

From Dev

Data structure for bidimensional array

From Dev

Data structure - Array

From Dev

Saving Javascript object that has an array of Parse.files causes "Converting circular structure to JSON" error

From Dev

Parse JSON Array where each member has different schema but same general structure

From Dev

Parse xml data from xsd data structure?

Related Related

  1. 1

    Golang: structure to generate/parse both XML and JSON

  2. 2

    Golang structure for json to allow a value to optionally be an array

  3. 3

    How to parse nested JSON data structure

  4. 4

    How to parse nested JSON data structure

  5. 5

    Parse JS array in JSON string as Golang map

  6. 6

    How to parse a json array with the same structure but different names

  7. 7

    How to parse JSON data without key names into a Java class structure?

  8. 8

    how to parse data from json Array

  9. 9

    Making minimal modification to JSON data without a structure in golang

  10. 10

    Golang - unable to parse JSON

  11. 11

    How to parse JSON arrays with two different data types into a struct in Golang

  12. 12

    How to parse JSON data without define array in JSON in Android Studio

  13. 13

    How to parse a structure array as a parameter?

  14. 14

    How to parse this Json structure in Java

  15. 15

    Unable to parse this json file in golang

  16. 16

    How can I deserialize a JSON array into a native .net data structure?

  17. 17

    "no implicit conversion of Array into String" when trying to parse JSON data

  18. 18

    Highcharts csv data loading and parse into separate array and json

  19. 19

    How can I parse json data as array in javascript?

  20. 20

    How to parse nested Json Object inside Array Data in Ios

  21. 21

    Parse data from uint8array to string to json

  22. 22

    Is this JSON structure an array or an object?

  23. 23

    JSON and array structure typeahead

  24. 24

    Is the array structure redundant in json?

  25. 25

    Data structure for bidimensional array

  26. 26

    Data structure - Array

  27. 27

    Saving Javascript object that has an array of Parse.files causes "Converting circular structure to JSON" error

  28. 28

    Parse JSON Array where each member has different schema but same general structure

  29. 29

    Parse xml data from xsd data structure?

HotTag

Archive