-1

I'm having trouble unmarshalling a nested json array. Sample below:

{
  "Subnets": [
    {
      "AvailabilityZone": "xx",
      "AvailabilityZoneId": "xx",
      "AvailableIpAddressCount": 173,
      "CidrBlock": "xx",
      "DefaultForAz": "xx",
      "MapPublicIpOnLaunch": "xx",
      "MapCustomerOwnedIpOnLaunch": "xx",
      "State": "xx",
      "SubnetId": "xx",
      "VpcId": "xx",
      "OwnerId": "xx",
      "AssignIpv6AddressOnCreation": "xx",
      "Ipv6CidrBlockAssociationSet": [],
      "Tags": [
        {
          "Key": "Name",
          "Value": "xx"
        },
        {
          "Key": "workload",
          "Value": "xx"
        },
        {
          "Key": "xx",
          "Value": "xx"
        },
        {
          "Key": "aws:cloudformation:stack-name",
          "Value": "xx"
        },
        {
          "Key": "host_support_group",
          "Value": "xx"
        },
        {
          "Key": "environment",
          "Value": "xx"
        },
        {
          "Key": "client",
          "Value": "xx"
        },
        {
          "Key": "aws:cloudformation:stack-id",
          "Value": "xx"
        },
        {
          "Key": "application",
          "Value": "Subnet"
        },
        {
          "Key": "xx",
          "Value": "xx"
        },
        {
          "Key": "xx",
          "Value": "xx"
        },
        {
          "Key": "xx",
          "Value": "xx"
        },
        {
          "Key": "regions",
          "Value": "ca-central-1"
        }
      ],
      "SubnetArn": "xx"
    }]
  ,
  "ResponseMetadata": {
    "RequestId": "xx",
    "HTTPStatusCode": 200,
    "HTTPHeaders": {
      "x-amzn-requestid": "xx",
      "cache-control": "no-cache, no-store",
      "strict-transport-security": "max-age=31536000; includeSubDomains",
      "content-type": "text/xml;charset=UTF-8",
      "content-length": "3176",
      "vary": "accept-encoding",
      "date": "xx",
      "server": "AmazonEC2"
    },
    "RetryAttempts": 0
  }
}

The only value I want is "AvailableIpAddressCount", I tried using interface{} but I'm not able to get the necessary value. Here's the Golang playground link - playground

Error- I'm getting this error using interface{}

Is there any other way to extract just the "AvailableIpAddressCount" value from the json object?

Any help or references are appreciated.

4
  • See if this helps Commented Sep 7, 2021 at 9:06
  • I tried using the .string() method but as the nested object is an array, it fails to covert it. Commented Sep 7, 2021 at 9:14
  • play.golang.com/p/dM3eIzPl21I Commented Sep 7, 2021 at 10:31
  • 1
    You should try like this - stackoverflow.com/a/69054742/16560548 Commented Sep 13, 2021 at 8:50

2 Answers 2

3

You can create a struct with the desired field and unmarshal the JSON bytes using that struct which will populate fields mentioned in your struct.

type Something struct {
    AvailableIpAddressCount int `json:"AvailableIpAddressCount"`
}

var data Something
if err := json.unmarshal(byt, &data); err != nil {
        panic(err)
}

AvailableIpAddressCount = data.AvailableIpAddressCount
Sign up to request clarification or add additional context in comments.

1 Comment

Remember to keep the first letter capital of the struct fields else unmarshal will not be able to populate the value.
1

Go uses static structs to decode json, so you probably need to create a struct that contains at least what you are looking for. If you have your struct, you can access AvailableIPAddressCount like this:

tmp.Subnets[0].AvailableIPAddressCount

Here is the playground https://play.golang.org/p/FsjeOubov1Q.

To create json structs from example json you can use tools like this.

And if you need to loop through all subnets:

for _, subnet := range tmp.Subnets {
    fmt.Println(subnet.AvailableIPAddressCount)
}

If you want to use json dynamically you can also use https://github.com/spf13/viper. But it's probably slower than statically decoding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.