Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Mynameisfred_64
Contributor
Contributor

Conditional JSON Structure

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

Labels (3)
3 Replies
Anonymous
Not applicable

Hi

Unfortunately, there is no such component to check and build the JSON String dynamically.

 

Regards

Shong

Mynameisfred_64
Contributor
Contributor
Author

Thanks Shong, any idea on how to achieve this without dedicated components ?

Anonymous
Not applicable

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.