Decoding Toplevel JSON array

Hi,

I would like to decode a toplevel JSON array. The rows bring me some confusion since there aren’t any columns at toplevel. Only a array / rows are available.

It tried many variations on my code, Googling for solutions but all without success. Hopefully someone can help me out.

Thanks in advance.

package main
import (
	"fmt"
    "encoding/json"

)
type TL []DataTest

type DataTest struct {
	userId int64 `json:"userId"`
	id int64 `json:"id"`
	title string `json:"title"`
	body string `json:"body"`
}

func main() {

	arrayBody := []byte(`[{
		"userId": 1,
		"id": 1,
		"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
		"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
	  },
	  {
		"userId": 1,
		"id": 2,
		"title": "qui est esse",
		"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
	  }]`)

	pm := TL{}

	json.Unmarshal(arrayBody,pm)

	fmt.Println(pm)

	for k := range pm  {
		fmt.Println( pm[k].userId );
	}

}

Could you please edit your question and indent your code? The </> button in the edit form helps you do that.

Right now your code is very hard to read.

1 Like
  1. You can only decode to exported fields of your struct.
  2. Unmarshal to &pm.

This code

package main

import (
    "encoding/json"
    "fmt"
)

type TL []DataTest

type DataTest struct {
    UserId int64  `json:"userId"`
    Id     int64  `json:"id"`
    Title  string `json:"title"`
    Body   string `json:"body"`
}

func main() {

    arrayBody := []byte(`[{
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
      }]`)

    pm := TL{}

    json.Unmarshal(arrayBody, &pm)

    fmt.Println(pm)

    for k := range pm {
        fmt.Println(pm[k].UserId)
    }

}

prints

[{1 1 sunt aut facere repellat provident occaecati excepturi optio reprehenderit quia et suscipit
suscipit recusandae consequuntur expedita et ■■■
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto} {1 2 qui est esse est rerum tempore vitae
sequi sint nihil reprehenderit dolor beatae ea dolores neque
fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis
qui aperiam non debitis possimus qui neque nisi nulla}]
1
1
3 Likes

Wow, thanks!

Didn’t know the struct variables had to be capitilized. Thank you for your quick response @lutzhorn!

Take a look at https://www.goinggo.net/2014/03/exportedunexported-identifiers-in-go.html to learn about exported identifiers.

1 Like

Also, check your errors. The return from json.Unmarshal tells you what is wrong, usually.

2 Likes

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