-1

I need to loop over the below Json array and transform the data into the expected output. However, I am facing problem in looping over the nested array.

input : 


   [{"DomainName":"Access Supplier",
   "Data_Category" :
  [{"Data_Category_Name":"System","Data_Category_AssetID":"2"}],
     "Data_Fields":
     [{"Field_Name":"First Name","WID":"7a"},
      {"Field_Name":"Last Name","WID":"213"}]},


  {"DomainName":"Access Talent",
"Data_Category":
  [{"Data_Category_Name":"Talent culture","Data_Category_AssetID":"54c"}],
   "Data_Fields":
  [{"Field_Name":"Key Resp","WID":"de7"},
    {"Field_Name":"Talent","WID":"043b5"}]}]

Expected Output:

  [{
"Field_Name": "First Name"
"Data_Category": [
{
 "Data_Category_AssetID": "2"
"Data_Category_Name": "System"
 }
],

"Field_Name":"Last Name"
"Data_Category": [
 {
"Data_Category_AssetID": "2"
"Data_Category_Name": "System"
 }],

 "Field_Name":"Key Resp"
 "Data_Category": [
{
"Data_Category_AssetID": "54c"
 "Data_Category_Name": "Talent culture"
   }],


  "Field_Name":"Talent"
 "Data_Category": [ 
   {
 "Data_Category_AssetID": "54c"
"Data_Category_Name": "Talent culture"
   }]



   }  ]
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

1 Answers1

1

You can use submap to copy only what you want from your Data_Fields (e.g. only Field_Name). Then assign all of those new maps the Data_Category from your source.

import groovy.json.JsonOutput

def data = ["Domain Name": "test", 
            "Data_Category":[["Data_Category_AssetID":"1","Data_Category_Name": "worker1"],
                             ["Data_Category_AssetID":"2","Data_Category_Name": "worker2"]],
            "Data_Fields":  [["WID":"1", "Field_Name":"First Name"],
                             ["WID":"2", "Field_Name":"Last Name"]]]

def allDataFields = data.Data_Fields.collect{
  it.subMap(["Field_Name"]).tap{
    Data_Category = data.Data_Category
  }
}

println(JsonOutput.prettyPrint(JsonOutput.toJson(allDataFields)))
// [
//     {
//         "Field_Name": "First Name",
//         "Data_Category": [
//             {
//                 "Data_Category_AssetID": "1",
//                 "Data_Category_Name": "worker1"
//             },
//             {
//                 "Data_Category_AssetID": "2",
//                 "Data_Category_Name": "worker2"
//             }
//         ]
//     },
//     {
//         "Field_Name": "Last Name",
//         "Data_Category": [
//             {
//                 "Data_Category_AssetID": "1",
//                 "Data_Category_Name": "worker1"
//             },
//             {
//                 "Data_Category_AssetID": "2",
//                 "Data_Category_Name": "worker2"
//             }
//         ]
//     }
// ]
cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Thanks, The avbove script doesnt work if we have multiple "Domain Name" – mohammmed sohail Jan 02 '21 at 05:06
  • input : [{"DomainName":"Access Supplier", "Data_Category" : [{"Data_Category_Name":"System","Data_Category_AssetID":"2"}], "Data_Fields": [{"Field_Name":"First Name","WID":"7a"}, {"Field_Name":"Last Name","WID":"213"}]}, {"DomainName":"Access Talent", "Data_Category": [{"Data_Category_Name":"Talent culture","Data_Category_AssetID":"54c"}], "Data_Fields": [{"Field_Name":"Key Resp","WID":"de7"}, {"Field_Name":"Talent","WID":"043b5"}]}] – mohammmed sohail Jan 02 '21 at 05:33
  • Expected output : [{ "Field_Name": "First Name" "Data_Category": [ { "Data_Category_AssetID": "2" "Data_Category_Name": "System" }], "Field_Name":"Last Name" "Data_Category": [ { "Data_Category_AssetID": "2" "Data_Category_Name": "System" }], "Field_Name":"Key Resp" "Data_Category": [{ "Data_Category_AssetID": "54c" "Data_Category_Name": "Talent culture" }], "Field_Name":"Talent" "Data_Category": [{ "Data_Category_AssetID": "54c" "Data_Category_Name": "Talent culture" }]}] – mohammmed sohail Jan 02 '21 at 05:34
  • 2
    Of course it works - it works for one element, you now just need to apply it to all your elements. If you simplify a problem, make sure, you don't simplify it so much, it no longer is your actual problem. Also if you have changes, please edit the question. And next: neither this nor the original "expected output" are valid JSON. – cfrick Jan 02 '21 at 10:03