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

Time:08-04

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": "[email protected]",
                "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": "[email protected]",
                "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)  
    
    }

CodePudding user response:

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

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

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

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".

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 "MeterNumber":
      realData.MeterNumber = cf.Value
    case "AccountNumber":
      realData.AccountNumber = cf.Value
    }
  }
  return nil
}

CodePudding user response:

Got it fixed!

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"`
}

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, &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
}

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

  • Related