LoginSignup
27
11

【Go】JSONから構造体が生成できるJSON-to-Goを紹介

Last updated at Posted at 2023-12-22

はじめに

こんにちは、なかじです!

GoでWEB開発する際、JSONデータを扱うことが多いと思います!

JSONデータをGoの構造体に変換するのは、面倒なときがあると思います。

そこで役立つのが、「JSON-to-Go」というツールです!

今回の記事では、この「JSON-to-Go」を紹介したいと思います!

😃 JSON-to-Goとは?

こちらが、今回紹介するJSON-to-Goです。

😃 使用方法

今回はダミーでデータを用意します

スクリーンショット
[
  {
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "name": "string",
    "email": "user@example.com",
    "createdAt": "2023-12-19T18:11:46.975Z",
    "profile": {
      "avatarUrl": "string",
      "bio": "string",
      "location": "string",
      "website": "string"
    },
    "roles": [
      {
        "roleId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "roleName": "string"
      }
    ],
    "settings": {
      "language": "string",
      "timezone": "string",
      "notificationsEnabled": true
    }
  }
]

ここから、

type AutoGenerated []struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	CreatedAt time.Time `json:"createdAt"`
	Profile   Profile   `json:"profile"`
	Roles     []Roles   `json:"roles"`
	Settings  Settings  `json:"settings"`
}

type Profile struct {
	AvatarURL string `json:"avatarUrl"`
	Bio       string `json:"bio"`
	Location  string `json:"location"`
	Website   string `json:"website"`
}

type Roles struct {
	RoleID   string `json:"roleId"`
	RoleName string `json:"roleName"`
}

type Settings struct {
	Language             string `json:"language"`
	Timezone             string `json:"timezone"`
	NotificationsEnabled bool   `json:"notificationsEnabled"`
}

が生成できます!

Inline type definitions(インライン型定義)

スクリーンショット 2023-12-21 16.31.18(2).png

JSONからGoのデータ構造への変換Goのデータ構造からJSONへの変換を行う際に、一時的な型を定義する方法です。

これにより、構造体を事前に定義することなく、直接型をインラインで定義してJSONとの相互作用を行うことができます。

上のコードだと、以下のように生成されます!

type AutoGenerated []struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	CreatedAt time.Time `json:"createdAt"`
	Profile   struct {
		AvatarURL string `json:"avatarUrl"`
		Bio       string `json:"bio"`
		Location  string `json:"location"`
		Website   string `json:"website"`
	} `json:"profile"`
	Roles []struct {
		RoleID   string `json:"roleId"`
		RoleName string `json:"roleName"`
	} `json:"roles"`
	Settings struct {
		Language             string `json:"language"`
		Timezone             string `json:"timezone"`
		NotificationsEnabled bool   `json:"notificationsEnabled"`
	} `json:"settings"`
}

omitempty

スクリーンショット 2023-12-21 16.31.21(2).png

Go言語でJSONのシリアライズ(構造体をJSONに変換)する際に使われるタグの一つです!

構造体のフィールドがゼロ値(空の文字列、0、nilなど)の場合に、そのフィールドをJSONに含めないようにするために使用されます。

type Example struct {
    Name    string `json:"name"`
    Age     int    `json:"age"`
    Address string `json:"address,omitempty"`
}

Address フィールドのタグについて、json:"address,omitempty" というタグが使われています。

Address フィールドがゼロ値(例えば空の文字列)であった場合、生成されるJSONにはaddressフィールドが含まれません。

JSONのサイズを小さく保つためや、不要なデータを送信しないために役立ちます!

😃 その他

VSCode版もありますw

姉妹ツールも(curl-to-Go

また、JSON-to-Goと同様に、curlコマンドをGoのコードに変換する「curl-to-Go」というツールも存在します!ぜひ!

おわりに

今回は、JSONから構造体が生成できるJSON-to-Goを紹介しました。

このツールのおかげで、JSONデータをGoの構造体に変換するのを楽にできそうですね!

PR

HRBrainでは一緒に働いてくれる仲間を募集しています!😁

27
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
11