Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
rid_rj
Contributor III
Contributor III

Get dynamic Key and value from json file

I'm a Talend developer and i have a json files that contain a dynamic part and static part, my problem is in the dynamic part because i can't get the keys so i change to create a java class to get the  keys.

my file is like this :

 

   [
    {
        "data": {
            "1801": "1801_13",
            "293954": "293954_1",
            "293985": "noir",
            "294071": "294071_3",
            "ean":  "5035915032563",
             "Nature": "450"            
        },
        
        "creation_date": "2018-08-28T12:25:06.638Z",
        "update_date": "2018-08-28T12:32:55.640Z",
       },

     {
        "data": {
            "2633": "1801_13",
            "293954": "293954_1",
            "ean":  "5035236232070",
            "Nature": "301"             
        },
        
        "creation_date": "2018-08-28T12:25:06.638Z",
        "update_date": "2018-08-28T12:32:55.640Z",
       }  ]


    

data : dynamic part, and in this part i have two static keys (ena and nature) and the others keys are dynamic like this example.
I create a many class java but without success.


i create this class java

      

 package routines;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.*;
    
    public class getKeyFromFile {
       
        
        
           
            public static void main(String filepath) throws ParseException, JSONException {
                JSONParser parser = new JSONParser();
              
                try {
             Object obj = parser.parse(new FileReader( filepath));
             JSONObject jsonObject = (JSONObject)obj ;
                        
              
                printJSON((JSONObject) jsonObject);
                } catch (FileNotFoundException e) {
    
                } catch (IOException ioe) {
    
                }       
            }
                public static void printJSON(JSONObject jsonObj) {
                    for (Object keyObj : jsonObj.keySet()) {
                        String key = (String)keyObj;
                        Object valObj = jsonObj.get(key);
                        if (valObj instanceof JSONObject) {
                            // call printJSON on nested object
                            printJSON((JSONObject) valObj);
                        } else {
                            // print key-value pair
                            System.out.println("key : "+key);
                            System.out.println("value : "+valObj.toString());
                        }
                    }
                }}




i get this error :

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray   

Can you help me please.    

Labels (4)
5 Replies
Anonymous
Not applicable

Hello,

Are you able to use tExtractRegexFields component to get your key value?

Best regards

Sabrina

Parikhharshal
Creator III
Creator III

@ridha_rjili: Did you end up getting any solution for this? Can you please share something?

Anjali3
Contributor
Contributor

Hi ,

 

Any Solution to this scenario. Im looking for way to parse the json value dynamically. Please help.

Anonymous
Not applicable

Hello,

Could you please elaborate your case with an example with input and expected output values? Which will be helpful for us to understand your requirement very well.

Best regards

Sabrina

Anjali3
Contributor
Contributor

Hi ,

 

My job should parse the JSON file to text file irrespective of the JSON structure. Job should dynamically parse it. Im trying to create a generic job for this use case. since I have 100 of JSON file with different structure. I don't want to create separate jobs for each of the file.