Search code examples
talendesb

Talend Extract Json String As Key value Array


Hi I work with Talend ESB 7.2 and i try to work on tRest Response

I have Already extract the Json using the tJsonExtractField tJsonExtractField Configuration

In "metas" field i have a another json String and I try to extract this String as array (with key value association).

exemple of meta field

{  
   "482f75dd-a8e4-4f5c-80f0-85b5a7eeb340":[  
      "indéterminé"
   ],
   "d8cac586-2d64-4fe6-bdf4-91a5bac3541e":[  
      "Id system"
   ],
   "b17baa47-2aa4-4959-a4f1-073f97833ba2":[  
      "Intranet_Télé_services_Web"
   ],
   "df35ac57-90eb-4e7f-af56-3f50be808ee1":[  
      "indéterminé"
   ],
   "046f1767-f303-4f89-bed6-7c58dab5f47b":[  
      "indéterminé"
   ],
   "ed979530-1dcc-4f48-853c-9dc43ebfc92d":[  
      "indéterminé"
   ],
   "a19f08a9-edf1-489b-9fb3-17a6335df112":[  
      "indéterminé"
   ],
   "60911596-67e7-4caf-afa1-67ff90c8fa8b":[  
      "indéterminé"
   ],
   "0dee3ca9-7962-4ee5-bc02-89d80e315072":[  
      "saas"
   ],
   "4e44268d-0231-4d1d-8e6d-13580dc89b31":[  
      "indéterminé"
   ],
   "0fd60a21-0bb4-4e3f-a203-8cef11ad3149":[  
      "indéterminé"
   ],
   "7c4b6cb3-1918-4ec5-85dc-add60186d29b":[  
      "indéterminé"
   ],
   "48657111-f80f-4bd1-842d-0ce2c4a044b6":[  
      "indéterminé"
   ],
   "6061a25d-3aaf-d58f-81a4-ad38878d2952":[  
      "indéterminé"
   ]
}

Edit 1 : Json.

I put you 2 exemple of 2000 items :

[  
   {  
      "icon":"fa fa-th",
      "infra":false,
      "dateMaj":1556582400000,
      "meta":{  
         "482f75dd-a8e4-4f5c-80f0-85b5a7eeb340":[  
            "indéterminé"
         ],
         "d8cac586-2d64-4fe6-bdf4-91a5bac3541e":[  
            "Abelium"
         ],
         "b17baa47-2aa4-4959-a4f1-073f97833ba2":[  
            "indéterminé"
         ],
         "df35ac57-90eb-4e7f-af56-3f50be808ee1":[  
            "Hyperfile"
         ],
         "046f1767-f303-4f89-bed6-7c58dab5f47b":[  
            "indéterminé"
         ],
         "ed979530-1dcc-4f48-853c-9dc43ebfc92d":[  
            "indéterminé"
         ],
         "a19f08a9-edf1-489b-9fb3-17a6335df112":[  
            "indéterminé"
         ],
         "60911596-67e7-4caf-afa1-67ff90c8fa8b":[  
            "indéterminé"
         ],
         "0dee3ca9-7962-4ee5-bc02-89d80e315072":[  
            "indéterminé"
         ],
         "4e44268d-0231-4d1d-8e6d-13580dc89b31":[  
            "Enfance"
         ],
         "0fd60a21-0bb4-4e3f-a203-8cef11ad3149":[  
            "indéterminé"
         ],
         "7c4b6cb3-1918-4ec5-85dc-add60186d29b":[  
            "indéterminé"
         ],
         "48657111-f80f-4bd1-842d-0ce2c4a044b6":[  
            "indéterminé"
         ],
         "6061a25d-3aaf-d58f-81a4-ad38878d2952":[  
            "19-LOG-037",
            "19-LOG-042",
            "19-LOG-043"
         ]
      },
      "history":{  

      },
      "id":"60d62577-e852-4f1c-8e33-19f64a38c511",
      "label":"DOMINO Web",
      "cartoVersion":"3.15.10",
      "code":"",
      "description":"My app 1 ",
      "teamleader":"[email protected]",
      "businesses":[  
         "e99164a0-c331-4fc0-9f90-af47e535b2c7",
         "94f526e9-f983-4278-99ed-59de62619cee",
         "d72828f9-2dd2-4ca7-bcd1-f22923ad09b3"
      ]
   },
   {  
      "icon":"fa fa-th",
      "infra":false,
      "dateMaj":1521676800000,
      "meta":{  
         "482f75dd-a8e4-4f5c-80f0-85b5a7eeb340":[  
            "indéterminé"
         ],
         "d8cac586-2d64-4fe6-bdf4-91a5bac3541e":[  
            "Id system"
         ],
         "b17baa47-2aa4-4959-a4f1-073f97833ba2":[  
            "Intranet_Télé_services_Web"
         ],
         "df35ac57-90eb-4e7f-af56-3f50be808ee1":[  
            "indéterminé"
         ],
         "046f1767-f303-4f89-bed6-7c58dab5f47b":[  
            "indéterminé"
         ],
         "ed979530-1dcc-4f48-853c-9dc43ebfc92d":[  
            "indéterminé"
         ],
         "a19f08a9-edf1-489b-9fb3-17a6335df112":[  
            "indéterminé"
         ],
         "60911596-67e7-4caf-afa1-67ff90c8fa8b":[  
            "indéterminé"
         ],
         "0dee3ca9-7962-4ee5-bc02-89d80e315072":[  
            "saas"
         ],
         "4e44268d-0231-4d1d-8e6d-13580dc89b31":[  
            "indéterminé"
         ],
         "0fd60a21-0bb4-4e3f-a203-8cef11ad3149":[  
            "indéterminé"
         ],
         "7c4b6cb3-1918-4ec5-85dc-add60186d29b":[  
            "indéterminé"
         ],
         "48657111-f80f-4bd1-842d-0ce2c4a044b6":[  
            "indéterminé"
         ],
         "6061a25d-3aaf-d58f-81a4-ad38878d2952":[  
            "indéterminé"
         ]
      },
      "history":{  

      },
      "id":"066a3fce-79e3-4a27-88c1-5217f2cd33f5",
      "label":"Id system",
      "cartoVersion":"3.15.10",
      "code":"",
      "description":"1 screen",
      "teamleader":"[email protected]",
      "businesses":[  
         "8cb8cb38-084f-4962-bd97-dc80b4d20880"
      ]
   }
]

Edit 2 : Expected. For outpout i don't find what is possible to do but if i can transform to a valid json structure will help for exemple :

{  
   "meta":[  
      {  
         "id":"482f75dd-a8e4-4f5c-80f0-85b5a7eeb340",
         "value":[  
            "indéterminé"
         ]
      },
      {  
         "id":"d8cac586-2d64-4fe6-bdf4-91a5bac3541e",
         "value":[  
            "Id system"
         ]
      }
   ]
}

Thanks for help


Solution

  • In a tLibraryLoad component load the json-simple jar And import needed librairy (in advanced setting).

    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    

    In a tJavaFlex component you can work on your data input (called onlyMet bellow)

    JSONObject job = new JSONObject(); 
    JSONParser parser = new JSONParser();
    
    try {
        //-- Parsing et prepare result
        List<JSONObject> resultList = new ArrayList<>();
        JSONObject jsonObjects = (JSONObject) parser.parse((String)onlyMet.metas);
    
        jsonObjects.keySet().parallelStream().forEach(entry -> {
            JSONObject rjo = new JSONObject();
            rjo.put("id", entry);
            rjo.put("value", jsonObjects.get(entry));
            // System.out.printf("- meta %s : %s \n", entry, jsonObjects.get(entry)); // for debug if needed
            resultList.add(rjo);
        });
    
        // Outpout the result
        onlyOut.metas = resultList.toString();
    
    } catch (final Exception e){
        e.printStackTrace();
    }