How to compare all the child ID with their parent ID, In a deeply nested JSON?

267 views Asked by At

This is the JSON document that i need to validate. I have to check if all the parent_id in the Children are correct. If all the parent and child Id is correct i will return a "valid" String.

{
  "id": 10,
  "children": [
    {
      "id": 25,
      "parent_id": 10,
      "children": [
        {
          "id": 131,
          "parent_id": 25,
          "children": null
        },
        {
          "id": 33,
          "parent_id": 25,
          "children": [
            {
              "id": 138,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 139,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 140,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 160,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 34,
              "parent_id": 33,
              "children": [
                {
                  "id": 26,
                  "parent_id": 34,
                  "children": null
                },
                {
                  "id": 64,
                  "parent_id": 34,
                  "children": null
                },
                {
                  "id": 65,
                  "parent_id": 34,
                  "children": null
                }
              ]
            },
            {
              "id": 35,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 36,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 38,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 39,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 40,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 41,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 42,
              "parent_id": 33,
              "children": null
            },
            {
              "id": 148,
              "parent_id": 33,
              "children": null
            }
          ]
        },
        {
          "id": 66,
          "parent_id": 25,
          "main_id": 66,
          "children": [
            {
              "id": 144,
              "parent_id": 66,
              "children": null
            },
            {
              "id": 146,
              "parent_id": 66,
              "children": null
            },
            {
              "id": 147,
              "parent_id": 66,
              "children": null
            },
            {
              "id": 67,
              "parent_id": 66,
              "children": null
            }
          ]
        }
      ]
    }
  ]
}

I used this code to iterate through. but was not successful in comparing. I got this code from one of the stack overflow questions on go. I'm a bit confused on where to start with.

func parseMap(aMap map[string]interface{}) {
    for key, val := range aMap {
        switch concreteVal := val.(type) {
        case map[string]interface{}:
            // if key == "id" {
            fmt.Println(key)
            // }
            parseMap(val.(map[string]interface{}))
        case []interface{}:
            // if key == "id" {
            fmt.Println(key)
            // }
            parseArray(val.([]interface{}))
        default:
                fmt.Println(key, ":", concreteVal)
            // if key == "parent_id" || key == "main_id" {

            // }
        }
    }
}

func parseArray(anArray []interface{}) {
    for i, val := range anArray {
        switch concreteVal := val.(type) {
        case map[string]interface{}:

            fmt.Println("Index:", i)
            parseMap(val.(map[string]interface{}))
        case []interface{}:
            fmt.Println("Index:", i)
            parseArray(val.([]interface{}))
        default:
            fmt.Println("Index", i, ":", concreteVal)
        }
    }
}

func main(w http.ResponseWriter, r *http.Request) {

    // data, err := ioutil.ReadAll(r.Body)
    m := map[string]interface{}{}

    err := json.Unmarshal([]byte(input), &m)
    if err != nil {
        panic(err)
    }
    parseMap(m)

}

Im getting the value from an API but for now i am hard codding the Json value

1

There are 1 answers

1
Flimpsy On BEST ANSWER

Define a type that matches the structure of your data:

type node struct {
    ID       int     `json:"id"`
    ParentID int     `json:"parent_id"`
    MainID   int     `json:"main_id"`
    Children []*node `json:"children"`
}

Write a recursive function to check the data:

func check(n *node) bool {
    for _, c := range n.Children {
        if c.ParentID != n.ID {
            return false
        }
        if !check(c) {
            return false
        }
    }
    return true
}

Unmarshal the JSON to a value of the type. Check the result.

var n node
err := json.Unmarshal(data, &n)
if err != nil {
    log.Fatal(err)
}
fmt.Println(check(&n))

Run it on the GoLang PlayGround.