5

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":"nicolas.gentil@abc.com",
      "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":"claude.dibus@abc.com",
      "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

pietro
  • 902
  • 10
  • 22
  • 1
    Could you please tell us what the expected output is ? and a complete json sample of your input would help too. – Ibrahim Mezouar Aug 23 '19 at 12:26
  • Hi @IbrahimMezouar I've edited with 2 full exemple of my json response. For outpout I don't know what can I do with that field value. – pietro Aug 23 '19 at 12:38
  • Could you add your expected output please ? – Ibrahim Mezouar Aug 23 '19 at 19:59
  • @IbrahimMezouar maby something as : [ { id : "0fd60a21-0bb4-4e3f-a203-8cef11ad3149", value : "[indéterminé]}, .... ] or equivalent – pietro Aug 26 '19 at 08:57
  • @[pietro](https://stackoverflow.com/users/3589856/pietro) the *value : ["indéterminé"]* seems to be a set of *String*. Is this true? – Meziane Sep 05 '19 at 12:02
  • Hi @Meziane yes it's an String `["Indeterminé"]` in json its can be an array of string but i can work with a complete String – pietro Sep 05 '19 at 14:20
  • @[pietro](https://stackoverflow.com/users/3589856/pietro): I deleted my answer. Sorry "J'ai besoin d'un café": I will try to look at it tomorrow, I must go home. – Meziane Sep 05 '19 at 14:39
  • @Meziane I kwon that :) thanks – pietro Sep 06 '19 at 06:07
  • @[pietro]() look at this: https://community.talend.com/t5/Design-and-Development/resolved-How-to-use-javascript-file/td-p/98087 If you can do that, I will repost my answer: it works 100% – Meziane Sep 06 '19 at 07:05
  • Thank you but I need to do it through the components because I have an input and I need to use the output too – pietro Sep 06 '19 at 08:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199062/discussion-between-meziane-and-pietro). – Meziane Sep 06 '19 at 09:00

1 Answers1

2

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();
}
pietro
  • 902
  • 10
  • 22
Meziane
  • 1,586
  • 1
  • 12
  • 22
  • Hi @meziane, I'm trying your code, import cannot work i should load libriary. Could you help for find the JSONParser – pietro Sep 09 '19 at 08:36
  • Hi @[pietro](https://stackoverflow.com/users/3589856/pietro), How can I help you? You mean *import java.io.FileReader;* and the like are not allowed in **Talend**? Try to add the *jar file* as in my answer – Meziane Sep 09 '19 at 09:46
  • json-simple is imported but paser doesn't work : http://prntscr.com/p3pgqw – pietro Sep 09 '19 at 10:06
  • Let me see your code-snippet if possible. I mean just the imports and the lines of codes where you use the Parser. – Meziane Sep 09 '19 at 10:07
  • i use `tJavaFlex` component and import instruction make compilation error : http://prntscr.com/p3pkh5, http://prntscr.com/p3pkwz – pietro Sep 09 '19 at 10:13
  • That means the json-simple.jar was not imported. Have you really added the right one to your project? There a lot of jars in the link I submitted: please choose the 13st file **json-simple-1.1.1.jar** – Meziane Sep 09 '19 at 10:16
  • I have retry by force your version i've the same error "unknow type" – pietro Sep 09 '19 at 12:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199180/discussion-between-pietro-and-meziane). – pietro Sep 09 '19 at 12:50