How to parse a json file with large recursive structures

Hi

I am new to go language. I am trying to read a json property, from the http response.
The response has a single record but it has recursive structures. I came across the solution where I can create a structure matching the json content. But in my case I don’t want to create a structure which could be very very complicated.
What other option do I have?

Thanks,
Rabi

Hello, @rabisankar_sahu.

It’d be good if you provided what you have tried so far and how deep is the structure so people could have a better understanding of the problem.

Thanks for your time.

This is what I have in my http response.
I am interested in getting value of token:id and user:abc:displayxyz
As I see only 1 way (via creating a structure) of doing it and I don’t want that way(because the structure is going to be very complicated), I have not tried anything else yet.

{
“token”: {
“id”: “eyJ0eXAiOiJKV1MiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0MDI4Yjg4MTU2MDkzMDc1MDE1NjA5MzA5ZGE4MDA3ZCIsInJlZnJlc2hfdG9rZW4iOiJxbThHMXpLN0pLSTFmTDdsMzlTT2wrL09QSXBKaStFQnZ1cXd1QTlCM015Sy9ZZ2dhZzZDSWNETExad2IzWVRibmdpYVRPek84RVNNd3QwV1ZMRy80Mjdlbk52UU1Fb01iWGxLR0dpeVdDM1d0QXl4ZmlGd3JPUk5Nb0Y1dlVXUnRxRDhvdEFRa3ZJMlJBaHZIM2M2QVBkY2kyL2VKTDE0TG9abEVrOHRiNDNCRTFNb3lhVWdSRmhKeldmUk1oVk5ZZi9PeGJjZlNQU29sNUM4bGJBZWdOZVlWblRWOGk1aGVocThOWG5LOUl2dHI3OWF2RWVNYldRVEordmdzT1diekZNMU8zU3dyK0tUbE1OL1VRNko3T1FMeGV2RXE5UkdqOXVYNFpDaTc5b09yQUZjYXZuUEx4MVBMTW54azFkRHZDQVdZMnpkci9tUjUzRTc5UGZHTTRQdW1TVDJRcHJRdm5PeG1EU0N1cEtIbGlsKzdvbmFpVGtYTkJEUUxKUnJJb1IrSUFEMFg4V1h4RWtUTG9qSlNwODQ4VDdzSUJaY2JDOFdCRGdOMFVxY2VRMzJadkxhMklheHRSNVV4VVhLdnFpVExtTFd5VzZZclIvbEJwQ3U2OExrM2xBek5semt5ZjhIWTJ2bDVrUT0iLCJpc3MiOiJJZE0gMS4xMC4xLTIwMTYwODEwLTY0IiwiY29tLmhwZS5pZG06dHJ1c3RvciI6bnVsbCwiZXhwIjoxNDcxNjUxMjc5LCJjb20uaHAuY2xvdWQ6dGVuYW50Ijp7ImlkIjoiNDAyOGI4ODE1NjA5MzA3NTAxNTYwOTMwOTkyOTAwNTUiLCJuYW1lIjoiQ09OU1VNRVIiLCJkZXNjcmlwdGlvbiI6IkNvbnN1bWVyIiwiZW5hYmxlZCI6dHJ1ZX0sInBybiI6ImNvbnN1bWVyIiwiaWF0IjoxNDcxNjQ5NDc5LCJqdGkiOiI0ZGI2NTI3MC1jNjk4LTQwODMtODk0NC1lZTc0YjRhYjZmZjQifQ.eDGxU9hRNOSfnMu9egzcnjB1WNQZWj6Usj-poGVJO4E”,
“issued_at”: “2016-08-19T23:31:19.492Z”,
“expires”: “2016-08-20T00:01:19.000Z”,
“tenant”: {
“id”: “4028b881560930750156093099290055”,
“xyz”: “CONSUMER”,
“description”: “Consumer”,
“enabled”: true
}
},
“user”: {
“userxyz”: “consumer”,
“id”: “4028b88156093075015609309da8007d”,
“xyz”: “consumer”,
“abc”: [
{
“id”: “propel_users_group_id”,
“xyz”: “propel-users”,
“displayxyz”: “Everyone”,
“metadata”: {},
“roles”: []
},
{
“id”: “4028b88156093075015609309cff0079”,
“xyz”: “CONSUMERGROUP”,
“displayxyz”: “zzzz consumer internal group”,
“groupInfo”: “zzzz://Organizations/Consumer/abc/zzzz consumer Internal abc”,
“metadata”: {
“detailed_description”: “zzzz consumer internal group for zzzz consumer organization”,
“icon_url”: “http://localhost/zzzzconsumer/consumergroup”,
“description”: “zzzz consumers internal group”,
“state”: “ACTIVE”,
“email”: "consumergroup@zzzzconsumer.com"
},
“roles”: []
}
],
“roles”: [
{
“id”: “4028b88156093075015609308c10000e”,
“locked”: false,
“type”: “CONSUMER”,
“integration”: false,
“application”: “zzzz”,
“xyz”: “SERVICE_CONSUMER”,
“displayxyz”: “zzzz Service Consumer”
},
{
“id”: “F91FB30178934E96A4447E73FF7FF24D”,
“locked”: true,
“type”: “CONSUMER”,
“application”: “zzzz”,
“xyz”: “ROLE_REST”,
“displayxyz”: “zzzz Role for REST API access”
},
{
“id”: “F91FB30178934E96A4447E73FF7FF24D”,
“locked”: true,
“type”: “CONSUMER”,
“application”: “zzzz”,
“xyz”: “ROLE_REST”,
“displayxyz”: “zzzz Role for REST API access”
}
],
“profile”: {
“distinguished_xyz”: “zzzz://Organizations/Consumer/users/consumer”,
“common_xyz”: “consumer”,
“email”: "consumer@zzzzconsumer.com",
“userxyz”: “consumer”
}
},
“refreshToken”: “qm8G1zK7JKI1fL7l39SOl+/OPIpJi+EBvuqwuA9B3MyK/Yggag6CIcDLLZwb3YTbngiaTOzO8ESMwt0WVLG/427enNvQMEoMbXlKGGiyWC3WtAyxfiFwrORNMoF5vUWRtqD8otAQkvI2RAhvH3c6APdci2/eJL14LoZlEk8tb43BE1MoyaUgRFhJzWfRMhVNYf/OxbcfSPSol5C8lbAegNeYVnTV8i5hehq8NXnK9Ivtr79avEeMbWQTJ+vgsOWbzFM1O3Swr+KTlMN/UQ6J7OQLxevEq9RGj9uX4ZCi79oOrAFcavnPLx1PLMnxk1dDvCAWY2zdr/mR53E79PfGM4PumST2QprQvnOxmDSCupKHlil+7onaiTkXNBDQLJRrIoR+IAD0X8WXxEkTLojJSp848T7sIBZcbC8WBDgN0UqceQ32ZvLa2IaxtR5UxUXKvqiTLmLWyW6YrR/lBpCu68Lk3lAzNlzkyf8HY2vl5kQ=”

Thanks,
Rabi

If you’re just interested in extracting a few key:value pairs then unmarshaling the JSON into a map[string]interface{} value can be simpler from a coding standpoint even though you do take a performance hit.

That’s a bit unformatted and annoying, but to extract the two mentioned paths it doesn’t seem too complicated to me.

type Whatever struct {
    Token struct {
        ID string
    }
    User struct {
        ABC []struct {
            DisplayXYZ string
        }
    }
}

var v Whatever
err := json.Unmarshal(data, &v)
// handle that error
fmt.Println(v.User.ABC[0].DisplayXYZ)

Untested etc, but the idea should be similar.

1 Like

Thanks Jakob. I tried your solution and it works as I was expecting without having to constructing the whole big fat structure

1 Like

Thanks Charles for your response. I will try your solution later.

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