[resolved] Empty/Null fields show up in json output twritejsonfield

Four Stars

[resolved] Empty/Null fields show up in json output twritejsonfield

I have a flow going into tWriteJsonFIELD which creates an array
{docs:}
However, if lets say the date is missing in one of the fields, it looks like this:
{docs:}
]}
Which is not what I want. If the field is empty/null I do not want it to show up in the output at all, like so:
{docs:}
Does anyone have any advice or ideas on how I can achieve this?
Thank you!

Accepted Solutions
Community Manager

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

Hi
It is impossible to generate the result as you expected by modifying anything on tWriteJasonField, try to replace null with another date or handle the string further after tWriteJasonField, for example, replace all the string ",\"date\":[]"with empty string.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business

All Replies
Community Manager

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

Hi
It is impossible to generate the result as you expected by modifying anything on tWriteJasonField, try to replace null with another date or handle the string further after tWriteJasonField, for example, replace all the string ",\"date\":[]"with empty string.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Four Stars

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

Good thinking Shong, I like the idea of just searching for the whole field and removing it altogether.
Thanks!
Four Stars

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

I added a tJavaRow after tWriteJSONField and did this:
output_row.output = input_row.output.replaceAll(",\"*\":\\", "");
Works like a charm.
Community Manager

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

I added a tJavaRow after tWriteJSONField and did this:
output_row.output = input_row.output.replaceAll(",\"*\":\\", "");
Works like a charm.

Cool, thanks for your feedback and the solution.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

BTW, for those, who faced similiar problem:
regex provided by jkrfs works for his case, but will not work if you have fields with underscore character or digits in name. Use this instead:
replaceAll(",?\"*\":\\", "");
Community Manager

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

BTW, for those, who faced similiar problem:
regex provided by jkrfs works for his case, but will not work if you have fields with underscore character or digits in name. Use this instead:
replaceAll(",?\"*\":\\", "");

Hi Andrey 
Thanks for offering a more comprehensive expression.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

I have a flow going into tWriteJsonFIELD which creates an array
{docs:}
However, if lets say the date is missing in one of the fields, it looks like this:
{docs:}
]}
Which is not what I want. If the field is empty/null I do not want it to show up in the output at all, like so:
{docs:}
Does anyone have any advice or ideas on how I can achieve this?
Thank you!

Hi,
How did you achieve the below using the twriteJson, Please assist. 
{docs:}

Thanks,
Community Manager

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

Hi,
How did you achieve the below using the twriteJson, Please assist. 
{docs:}

Thanks,

Hello Naina
It seems your problem is not related to this subject. In order to better manage and follow up your questions, please report a topic for your question. 
Regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Empty/Null fields show up in json output twritejsonfield

I'm wondering how the behavior reported here isn't a bug. When the values are empty, how does it make sense to convert it to []? This is especially bad because, at the opposite end, tExtractJSONFields doesn't recognize those [], and either turns it into a literal string of "[]" or (for numbers) throws an exception. I added this hack, and it's working. Is there no option to avoid the hack?