[ASK] Decoding json to slices but the keys is dynamic

Hi guys,

I want to read data from json and convert it into slices. i already surf the google but all of them just gave me an example for static key. in my case, i need to grab slices inside dynamic key.

here is the json response :

{
   "4.7":{
      "release_date":"2016-12-06",
      "changelog_url":"https://codex.wordpress.org/Version_4.7",
      "vulnerabilities":[
         {
            "id":8714,
            "title":"WordPress 4.3-4.7 - Potential Remote Command Execution (RCE) in PHPMailer",
            "created_at":"2017-01-12T08:39:27.000Z",
            "updated_at":"2017-01-16T08:53:22.000Z",
            "published_date":"2017-01-11T00:00:00.000Z",
            "references":{
               "url":[
                  "https://www.wordfence.com/blog/2016/12/phpmailer-vulnerability/",
                  "https://github.com/PHPMailer/PHPMailer/wiki/About-the-CVE-2016-10033-and-CVE-2016-10045-vulnerabilities",
                  "https://wordpress.org/news/2017/01/wordpress-4-7-1-security-and-maintenance-release/"
               ]
            },
            "vuln_type":"RCE",
            "fixed_in":"4.7.1"
         }
      ]
   }
}

note : key 4.7 is dynamic.

and here is my current progress :

package main

import (
	"io/ioutil"
	"os"

	"encoding/json"
	"fmt"
)

type Results struct {
	Four_7 struct {
		ChangelogURL    string `json:"changelog_url"`
		ReleaseDate     string `json:"release_date"`
		Vulnerabilities []struct {
			CreatedAt     string `json:"created_at"`
			FixedIn       string `json:"fixed_in"`
			ID            int    `json:"id"`
			PublishedDate string `json:"published_date"`
			References    struct {
				URL []string `json:"url"`
			} `json:"references"`
			Title     string `json:"title"`
			UpdatedAt string `json:"updated_at"`
			VulnType  string `json:"vuln_type"`
		} `json:"vulnerabilities"`
	} `json:"4.7"`
}

func main() {
	file := openFile("response.txt")
	res := &Results{}
	_ = json.Unmarshal([]byte(file), res)
	fmt.Println(res)
}

func openFile(file string) string {
	zfile, _ := os.Open(file)
	data, _ := ioutil.ReadAll(zfile)
	zfile.Close()
	return string(data)
}

any help would be appreciated :slight_smile:

regards

Hello!

Well, that’s easy: unpack to a map[string]yourStruct!

Example:

package main

type myStruct struct {
	ChangelogURL    string `json:"changelog_url"`
		ReleaseDate     string `json:"release_date"`
		Vulnerabilities []struct {
			CreatedAt     string `json:"created_at"`
			FixedIn       string `json:"fixed_in"`
			ID            int    `json:"id"`
			PublishedDate string `json:"published_date"`
			References    struct {
				URL []string `json:"url"`
			} `json:"references"`
			Title     string `json:"title"`
			UpdatedAt string `json:"updated_at"`
			VulnType  string `json:"vuln_type"`
		} `json:"vulnerabilities"`
}

func main() {
  const example = `your json example`
  var target map[string]myStruct
  if err := json.Unmarshal(example, &target); err != nil {
    fmt.Println("oops: ", err)
    os.Exit(1)
  }
  fmt.Println(target)
}

Edit: go playground link

1 Like

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