Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello
I'm struggling to get a "dynamic" json structure, depending on some input field value :
Ex :
{"field1" : "value1",
"field2" : value2,
"field3": value3}
Field2 is a numeric => when i get a zero or null value, i want to remove this field from the json structure, like this :
{"field1" : "value1", "field3": "value3"}
1- At first i ve used a tmap to test field 2 value and output accordingly to 2 twritejsonfield with 2 different structures
InputData--->Tmap (test value2) ---->tWriteJsonField1 (with structure : field1,field2,field3)
----->tWriteJsonField2 (with structure : field1,field3 , when value2 is null or zero)
Problem is that i have a big json structure, with multiple numeric fields that need to be tested and removed if they're null.
I don't want to create dozens "static" json structures in multiple tWriteJsonFields, to manage my bad data quality.
2 - My second solution was to create in a tJavaFlow the Json String , and adding "field2":value2 when row.value2!=0
I'm not satisfied with these solutions
Is there a way to use a "json object" in tJavaRow for exemple to build dynamically the structure and add the field only if conditions are fullfilled ?
Or a more simple solution with Talend native or user components that allows to add conditionally an attribute to the Json Tree
It might be very simple but i spent really too much time looking for the good way to do this and didn't succeed !
Million thanks if you can help me!
Fred
Hi
Unfortunately, there is no such component to check and build the JSON String dynamically.
Regards
Shong
Thanks Shong, any idea on how to achieve this without dedicated components ?
if you are a Java developer, I would like the way 2 mentioned in your post, write a user function to parse the JSON string and remove the fields whose value is zero or null, and the function return a JSON string.