DarkMantis
DarkMantis

Reputation: 1516

How to deal with a nested struct with the same elements in Go

I have been writing this code for a small project of mine and I want to parse some JSON data which looks like this:

{
    "payloads": [
        {
            "xss": [
                {
                    "payload": "{{RANDSTR}}\"><scRiPT>alert(1)</ScrIPT>{{RANDSTR}}",
                    "search": "{{RANDSTR}}\"><scRi"
                },
                {
                    "payload": "{{RANDSTR}}\"",
                    "search": "{{RANDSTR}}\""
                },
                {
                    "payload": "{{RANDSTR}}'",
                    "search": "{{RANDSTR}}'"
                }
            ],
            "tpli": [
                {
                    "payload": "{{RANDSTR}}${{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{{ {{RANDINT}} * {{RANDINT}} }}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{! {{RANDINT}} * {{RANDINT}} !}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                },
                {
                    "payload": "{{RANDSTR}}{% {{RANDINT}} * {{RANDINT}} %}",
                    "search": "{{RANDSTR}}{{RANDINT}}"
                }
            ]
        }
    ]
}

This is my struct declaration:


type Payload struct {
    Payload []struct {
        Payload string `json:"payload"`
        Search  string `json:"search"`
    }
}

type Payloads struct {
    Payloads []Payload `json:"payloads"`
}

I know this isn't how I'm supposed to do it but I'm not sure the best way. I don't want to specify the keys (xss, tpli, etc) as I want to easily expand this file without having to modify the Go file.

Can someone point me in the right direction on how to achieve this?

Thanks in advance.

Upvotes: 1

Views: 180

Answers (1)

blackgreen
blackgreen

Reputation: 44587

Model it as such:

type Payloads struct {
    Payloads []map[string][]Payload `json:"payloads"`
}

type Payload struct {
    Payload string `json:"payload"`
    Search  string `json:"search"`
}

Playground: https://play.golang.org/p/S6nnOKkADUO

Upvotes: 5

Related Questions