如何在Golang中将嵌套的JSON解组为简单的JSON键值对?

huangapple go评论91阅读模式
英文:

How can I unmarshall nested json to become simple json pairs Golang?

问题

嗨,请问我能帮到你什么?你想要将嵌套的JSON转换为简单的JSON对吗?你提供的示例中有两个不同的JSON结构,你想要将哪一个转换为简单的JSON对呢?请提供更多的信息,以便我能更好地帮助你。

英文:

Hi please I need some help, I'm coming in from nodejs and looking for a way to unmarshall a nested json so that it becomes simple json pairs.
This is a sample of my nested json.

{
    "id": 1000004,
    "reference": "T0000",
    "amount": 700000,
    "paid_at": "2022-07-24T00:49:13.000Z",
    "created_at": "2022-07-24T00:49:08.000Z",
    "Metadata": {
        "Custom_fields": [
            {
                "display_name": "MeterNumber",
                "value": "123456789"
            },
            {
                "display_name": "AccountNumber",
                "value": "123456"
            }
        ]
    }
}

I want to achieve this:

   {
    "id": 1000004,
    "reference": "T0000",
    "amount": 700000,
    "paid_at": "2022-07-24T00:49:13.000Z",
    "created_at": "2022-07-24T00:49:08.000Z",
    "meter_number": "123456789",
    "account_number": "123456",
}

it used to be a deep root nested payload, I have used the struct type to isolate the relevant pairs and array but it's still not in the format I need it to be for passing to DB.
Pointers and corrections will be appreciated.

How can I unmarshall nested json to become simple json pairs Golang?

This is the original data

    Data = []byte(`{
"status": true,
"message": "Transactions retrieved",
"data": [
{
"id": 10000000,
"domain": "test",
"status": "success",
"reference": "T0000000000",
"amount": 700000,
"message": null,
"gateway_response": "Successful",
"paid_at": "2022-07-24T00:49:13.000Z",
"created_at": "2022-07-24T00:49:08.000Z",
"channel": "card",
"currency": "NGN",
"ip_address": "102.89.32.12",
"metadata": {
"custom_fields": [
{
"display_name": "MeterNumber",
"variable_name": "meternumber",
"value": "123456789"
},
{
"display_name": "AccountNumber",
"variable_name": "accountnumber",
"value": "123456"
}
],
"referrer": "https://example.com/"
},
"log": {
"start_time": 1658623749,
"time_spent": 4,
"attempts": 1,
"errors": 0,
"success": true,
"mobile": false,
"input": [],
"history": [
{
"type": "action",
"message": "Attempted to pay with card",
"time": 2
},
{
"type": "success",
"message": "Successfully paid with card",
"time": 4
}
]
},
"fees": 20500,
"fees_split": null,
"customer": {
"id": 87000000,
"first_name": "foo",
"last_name": "foo",
"email": "foo@gmail.com",
"phone": "08090000000",
"metadata": null,
"customer_code": "CUS_foo",
"risk_action": "default"
},
"authorization": {
"authorization_code": "AUTH_foo",
"bin": "40123456789",
"last4": "40000",
"exp_month": "12",
"exp_year": "2030",
"channel": "card",
"card_type": "visa ",
"bank": "TEST BANK",
"country_code": "NG",
"brand": "visa",
"reusable": true,
"signature": "SIG_foo",
"account_name": null
},
"plan": {},
"split": {},
"subaccount": {},
"order_id": 800000,
"paidAt": "2022-07-24T00:49:13.000Z",
"createdAt": "2022-07-24T00:49:08.000Z",
"requested_amount": 700000,
"source": {
"source": "checkout",
"type": "web",
"identifier": null,
"entry_point": "request_inline"
},
"pos_transaction_data": null
},
{
"id": 1000000,
"domain": "test",
"status": "success",
"reference": "T13456789",
"amount": 500000,
"message": null,
"gateway_response": "Successful",
"paid_at": "2022-07-24T00:28:34.000Z",
"created_at": "2022-07-24T00:28:08.000Z",
"channel": "card",
"currency": "NGN",
"ip_address": "192.168.1.1",
"metadata": {
"custom_fields": [
{
"display_name": "MeterNumber",
"variable_name": "meternumber",
"value": "123456789"
},
{
"display_name": "AccountNumber",
"variable_name": "accountnumber",
"value": "123456"
}
],
"referrer": "https://example.com"
},
"log": {
"start_time": 1658622488,
"time_spent": 26,
"attempts": 1,
"errors": 0,
"success": true,
"mobile": false,
"input": [],
"history": [
{
"type": "action",
"message": "Set payment method to: bank",
"time": 15
},
{
"type": "action",
"message": "Set payment method to: ussd",
"time": 16
},
{
"type": "action",
"message": "Set payment method to: bank",
"time": 19
},
{
"type": "action",
"message": "Set payment method to: card",
"time": 22
},
{
"type": "action",
"message": "Attempted to pay with card",
"time": 25
},
{
"type": "success",
"message": "Successfully paid with card",
"time": 26
}
]
},
"fees": 17500,
"fees_split": null,
"customer": {
"id": 870000000,
"first_name": "foo",
"last_name": "foo",
"email": "foo@gmail.com",
"phone": "0809000000",
"metadata": null,
"customer_code": "CUS_xxxx",
"risk_action": "default"
},
"authorization": {
"authorization_code": "AUTH_xxxx",
"bin": "400000",
"last4": "40000",
"exp_month": "12",
"exp_year": "2030",
"channel": "card",
"card_type": "visa ",
"bank": "TEST BANK",
"country_code": "NG",
"brand": "visa",
"reusable": true,
"signature": "xxxxxx",
"account_name": null
},
"plan": {},
"split": {},
"subaccount": {},
"order_id": 827512,
"paidAt": "2022-07-24T00:28:34.000Z",
"createdAt": "2022-07-24T00:28:08.000Z",
"requested_amount": 500000,
"source": {
"source": "checkout",
"type": "web",
"identifier": null,
"entry_point": "request_inline"
},
"pos_transaction_data": null
}
],
"meta": {
"total": 2,
"total_volume": 12000,
"skipped": 0,
"perPage": 50,
"page": 1,
"pageCount": 1
}
}`)

here's what i did:

    type Uservariabledata struct {
//json.RawMessage `json:"custom_field"`	
Display_name	string  `json:"display_name"`
Value	string  `json:"value"`
}
//  struct
type UserDataDetail struct {
Custom_fields []Uservariabledata
}
//  struct
type UserData struct {
Id 	int  `json:"id"`
Reference string `json:"reference"`
Amount uint `json:"amount"`
Paid_at string `json:"paid_at"`
Created_at string `json:"created_at"`
Metadata  UserDataDetail
}
// Payload struct
type Payload struct {
Status        bool    `json:"status"`
Message       string    `json:"message"`
Data          []UserData
Meta		   json.RawMessage     `json:"meta"`
}
func (p *Payload) UnmarshalJSON(data []byte) error {
type payload struct {
Status        bool    `json:"status"`
Message       string    `json:"message"`
Data          []UserData
Meta		   json.RawMessage     `json:"meta"`
}
payld := &payload{}
err := json.Unmarshal(data, payld)
if err != nil {
return err
}
p.Status = payld.Status
p.Message = payld.Message
p.Data=payld.Data
p.Meta=payld.Meta
return nil
}
// GetUserAmountPaid endpoint, this was done in my controller.go
func GetUserAmountPaid(c echo.Context) (err error) {
respData := Data
payld := Payload{}
err = json.Unmarshal(respData, &payld)
if err != nil {
panic(err)
}
return c.JSON(http.StatusOK, payld.Data)  
}
</details>
# 答案1
**得分**: 1
你大部分已经确定了你想要为“真实数据”和“传输数据”使用单独的结构体。
```go
type RealData struct {
Id             int    `json:"id"`
...
MeterNumber    string `json:"meter_number"`
AccountNumber  string `json:"account_number"`
}

关键在于你的“传输数据”不太正确,它需要与被解析的JSON格式匹配。

type TransferData struct {
  Id int
  // ...
  Metadata struct {
    Custom_fields []struct {
      Display_name  string
      Variable_name string
      Value         string
    }
  }
}

然后,只需要将解组为“传输数据”类型的对象,并将其复制到你的“真实数据”中。

func (realData *RealData) UnmarshalJSON(data []byte) error {
  var transferData TransferData
  err := json.Unmarshal(data, &transferData)
  if err != nil {
    return err
  }
  realData.Id = transferData.Id
  // ...

  // 现在,我们必须将我们关心的已知自定义元数据字段挑选到我们真实数据的所需字段中。
  for _, cf := range transferData.Metadata.Custom_fields {
    switch cf.Display_name {
    case "MeterNumber":
      realData.MeterNumber = cf.Value
    case "AccountNumber":
      realData.AccountNumber = cf.Value
    }
  }
  return nil
}
英文:

You've mostly identified that you want to have separate structs for the "real data" and the "transferred data".

<!-- language: lang-go -->

type RealData struct {
Id int `json:&quot;id&quot;`
...
MeterNumber   string `json:&quot;meter_number&quot;`
AccountNumber string `json:&quot;account_number&quot;`
}

The trick is that your "transfer data" isn't quite right, it needs to match the format of the JSON being parsed.

<!-- language: lang-go -->

type TransferData struct {
Id int
// ...
Metadata struct {
Custom_fields []struct{
Display_name  string
Variable_name string
Value         string
}
}
}

Then it's just a matter of unmarshaling into an object of the "transfer data" type and copying it into your "real data".

<!-- langauge: lang-go -->
func (realData *RealData) UnmarshalJSON(data []byte) error {
var transferData TransferData
err := json.Unmarshal(data, &transferData)
if err != nil {
return err
}
realData.Id = transferData.Id
// ...

  // Now we must cherry-pick the known custom metadata fields we
// care about into the desired fields of our real data.
for _, cf := range transferData.Metadata.Custom_fields {
switch cf.Display_name {
case &quot;MeterNumber&quot;:
realData.MeterNumber = cf.Value
case &quot;AccountNumber&quot;:
realData.AccountNumber = cf.Value
}
}
return nil
}

答案2

得分: 0

修复好了!

type RealData struct {
Id int `json:"id"`
Reference string `json:"reference"`
Amount uint `json:"amount"`
Paid_at string `json:"paid_at"`
Created_at string `json:"created_at"`
MeterNumber string `json:"meter_number"`
AccountNumber string `json:"account_number"`
}

然后为了匹配我的真实数据的JSON

type TransferData struct {
Status bool
Message string
Data []struct {
Id int
Reference string
Amount uint
Paid_at string
Created_at string
Metadata struct {
Custom_fields []struct {
Display_name string
Variable_name string
Value string
}
}
}
Meta json.RawMessage
}

最后进行编组:

func (realData *RealData) UnmarshalJSON(data []byte) error {
var transferData TransferData
err := json.Unmarshal(data, &transferData)
if err != nil {
return err
}
for _, td := range transferData.Data {
realData.Id = td.Id
realData.Reference = td.Reference
realData.Amount = td.Amount
realData.Paid_at = td.Paid_at
realData.Created_at = td.Created_at
for _, rd := range td.Metadata.Custom_fields {
switch rd.Display_name {
case "MeterNumber":
realData.MeterNumber = rd.Value
case "AccountNumber":
realData.AccountNumber = rd.Value
}
}
}
return nil
}

感谢maeric提供的初始解决方案,我进行了升级。
希望这对某人有所帮助。

英文:

Got it fixed!

type RealData struct {
Id int `json:&quot;id&quot;`
Reference string `json:&quot;reference&quot;`
Amount uint `json:&quot;amount&quot;`
Paid_at string `json:&quot;paid_at&quot;`
Created_at string `json:&quot;created_at&quot;`
MeterNumber   string `json:&quot;meter_number&quot;`
AccountNumber string `json:&quot;account_number&quot;`
}

Then for matching the JSON for my real data

type TransferData struct {
Status        bool  
Message       string
Data          []struct{
Id 	int
Reference string
Amount uint
Paid_at string
Created_at string
Metadata struct {
Custom_fields []struct{
Display_name  string
Variable_name string
Value         string
}
}
}
Meta		  json.RawMessage 
}

and finally marshaling it:

func (realData *RealData) UnmarshalJSON(data []byte) error {
var transferData TransferData
err := json.Unmarshal(data, &amp;transferData)
if err != nil {
return err
}
for _, td := range transferData.Data {
realData.Id = td.Id
realData.Reference = td.Reference
realData.Amount = td.Amount
realData.Paid_at = td.Paid_at
realData.Created_at = td.Created_at
for _, rd := range td.Metadata.Custom_fields {
switch rd.Display_name {
case &quot;MeterNumber&quot;:
realData.MeterNumber = rd.Value
case &quot;AccountNumber&quot;:
realData.AccountNumber = rd.Value
}
}
}
return nil
}

Thanks to maeric for providing the initial solution which I upgraded.
I hope this helps someone.

huangapple
  • 本文由 发表于 2022年8月3日 01:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/73211377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定