-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"
   }]



   }  ]

1 Answer 1

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"
//             }
//         ]
//     }
// ]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, The avbove script doesnt work if we have multiple "Domain Name"
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" }]}]
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.