Extracting "values", and "tuples" nested inside a JSON

Extracting "values", and "tuples" nested inside a JSON

Hello, 

I have some inquires I would like to make in reference to the code below,

We are using Blancco API, to populate reports from Blancco into our form, we are using the getJson() function to populate the data;

I have been able to extract the data up on till "target" array, but the issue occurring, is that my method of extraction is not sustainable, because I am using "for loop", "if" statements, and "contains()", so when "keys" appears multiple times but with different "values" nested as tuples, it only returns the last tuple, and ignores the rest.

I am trying to extract all the "values" nested inside the "tuples" nested inside "target" array.

Example; The "values" nested inside "target_id", "internal_id", "controller_internal_id" etc.

How do I solve this problem?

This code below shows, what I have done in trying to solve that problem for "target", but it's returning only the "keys", and not the "values";

blancco_erasure_report = response.getJSON("reports")
.getJSON("blancco_data").getJSON("blancco_erasure_report");
// ###################################################################
erasures = response.getJSON("reports").getJSON("blancco_data")
.getJSON("blancco_erasure_report").getJson("erasures");
// ###################################################################
erasure = response.getJSON("reports").getJSON("blancco_data").getJSON("blancco_erasure_report")
.getJson("erasures").getJson("erasure");
// ###################################################################
    m = Map();
    l = List();
    for each  erasure_info in erasure
    {
        if(erasure_info.contains("erasure_id"))
        {
            erasure_type1 = erasure_info.getJson("@type");
            erasure_erasure_id = erasure_info.getJson("erasure_id");
        }
        if(erasure_info.contains("timestamp"))
        {
            erasure_type2 = erasure_info.getJson("@type");
            erasure_timestamp = erasure_info.getJson("timestamp");
        }
        // EXTRACTION OF "TARGET" CODE BELOW
        for each  rec1 in erasure_info.getJson("target")
        {
            if(rec1.keys().tostring().remove("@type,") != {"region","supported_formats","features"})
            {
                l.add(rec1.keys().tostring().remove("@type,"));
            }
            if(rec1.keys().tostring().remove("@type,") == "region")
            {
                m1 = Map();
                l1 = list();
                //l99 = list();
                for each  rec3 in rec1.getjson("region")
                {
                    l1.add(rec3.keys().tostring().remove("@type,"));
                    info "l: " + l;
                }
                m1.put("regions",l1);
                l.add(m1);
            }
            if(rec1.keys().tostring().remove("@type,") == "features")
            {
                m2 = Map();
                l2 = list();
                for each  rec4 in rec1.getjson("features")
                {
                    l2.add(rec4.keys().tostring().remove("@type,"));
                }
                m2.put("features",l2);
                l.add(m2);
            }
            if(rec1.keys().tostring().remove("@type,") == "supported_formats")
            {
                m3 = Map();
                l3 = list();
                for each  rec4 in rec1.getjson("supported_formats")
                {
                    m4 = Map();
                    l4 = list();
                    for each  rec5 in rec4.getjson("format")
                    {
                        l4.add(rec5.keys().tostring().remove("@type,"));
                    }
                    m4.put("format",l4);
                    l3.add(m4);
                }
                m3.put("supported_formats",l3);
                l.add(m3);
            }
        }
    }
    // PLEASE NOTE;
    //info "l: " + l; // Note: "l" is the whole "target"
    info "----------------------------";
    //info "l1: " + l1; // Note: "l1" is the data nested inside the "region" <key>
    info "----------------------------";
    //info "m1: " + m1; // Note: "m1" is the whole "region"
    info "----------------------------";
    //info "m2: " + m2; // Note: "m2" is the whole "features"
    info "----------------------------";
    //info "l2: " + l2; // Note: "l2" is the data nested inside the "features" <key>
    info "----------------------------";
    //info "m3: " + m3; // Note: "m3" is the whole "supported_formats"
    info "----------------------------";
    //info "m4: " + m4; // Note: "m4" is the whole "format" nested inside "supported_formats"
    info "----------------------------";
    //info "l4: " + l4; // Note: "l4" is the data nested inside the "format" <key>
    info "----------------------------";
    // ###################################################################
m.put("target",l);

This is the Full code below; 

{
 "reports": [
    {
      "blancco_data": {
        "description": {
          "document_id": "Redacted",
          "document_log": [
            {
              "author": {
                "product_name": {
                  "@id": "30",
                  "@name": "Redacted",
                  "value": "Redacted"
                },
                "product_version": "Redacted",
                "product_revision": "Redacted"
              },
              "date": "2024-04-24T10:26:08+0000",
              "clock_source": "Redacted"
            }
          ],
          "description_entries": {
            "company_information": [
              {
                "@type": "string",
                "customer_license": "Redacted"
              },
              {
                "@type": "string",
                "erasure_provider": "Redacted"
              }
            ],
            "license_consumption_ids": [
              {
                "@type": "string",
                "license_consumption_id": "Redacted"
              }
            ]
          }
        },
        "blancco_erasure_report": {
          "erasures": [
            {
              "erasure": [
                {
                  "@type": "uint",
                  "erasure_id": 1
                },
                {
                  "@type": "string",
                  "timestamp": "Redacted"
                },
                {
                  "target": [
                    {
                      "@type": "uint",
                      "target_id": 2
                    },
                    {
                      "@type": "uint",
                      "internal_id": 42
                    },
                    {
                      "@type": "uint",
                      "controller_internal_id": 29
                    },
                    {
                      "@type": "string",
                      "type": "disk"
                    },
                    {
                      "@type": "string",
                      "model": "Redacted"
                    },
                    {
                      "@type": "string",
                      "vendor": "Redacted"
                    },
                    {
                      "@type": "string",
                      "serial": "Redacted"
                    },
                    {
                      "@type": "string",
                      "oui": "Redacted"
                    },
                    {
                      "@type": "string",
                      "firmware_revision": "11200108"
                    },
                    {
                      "@type": "uint",
                      "blocksize": 512
                    },
                    {
                      "@type": "string",
                      "interface_type": "Redacted"
                    },
                    {
                      "@type": "uint",
                      "capacity": 123456789012
                    },
                    {
                      "@type": "uint",
                      "sectors": 123456789012
                    },
                    {
                      "region": [
                        {
                          "@type": "string",
                          "type": "Redacted"
                        },
                        {
                          "@type": "uint",
                          "capacity": 123456789012
                        },
                        {
                          "@type": "uint",
                          "sectors": 123456789012
                        },
                        {
                          "@type": "string",
                          "status": "Redacted"
                        }
                      ]
                    },
                    {
                      "@type": "string",
                      "health": "good"
                    },
                    {
                      "@type": "uint",
                      "namespace_id": 1
                    },
                    {
                      "@type": "uint",
                      "namespaces_size": 1
                    },
                    {
                      "@type": "string",
                      "namespace_eui": "Redacted"
                    },
                    {
                      "@type": "string",
                      "namespace_nguid": "Redacted"
                    },
                    {
                      "@type": "string",
                      "metadata_protocol": "Redacted"
                    },
                    {
                      "@type": "string",
                      "metadata_support": "Redacted"
                    },
                    {
                      "@type": "uint",
                      "metadata_blocksize": 0
                    },
                    {
                      "supported_formats": [
                        {
                          "format": [
                            {
                              "@type": "uint",
                              "index": 0
                            },
                            {
                              "@type": "uint",
                              "sector_size": 517
                            },
                            {
                              "@type": "uint",
                              "metadata_size": 0
                            }
                          ]
                        },
                        {
                          "format": [
                            {
                              "@type": "uint",
                              "index": 1
                            },
                            {
                              "@type": "uint",
                              "sector_size": 4094
                            },
                            {
                              "@type": "uint",
                              "metadata_size": 0
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "@type": "uint",
                      "selected_format": 0
                    },
                    {
                      "features": [
                        {
                          "@type": "string",
                          "feature": "Redacted"
                        },
                        {
                          "@type": "string",
                          "feature": "Redacted"
                        },
                        {
                          "@type": "string",
                          "feature": "Redacted"
                        },
                        {
                          "@type": "string",
                          "feature": "Redacted"
                        },
                        {
                          "@type": "string",
                          "feature": "Redacted"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  ]
}