Five Stars

twritejsonfield array when Null

I am using a tWriteJSONField on my job. I have several string inputs in my schema, but when one of those strings is empty/null it outputs an empty array instead of an empty string. The incoming string is not an array so I am not sure why it is outputing it as such. Can I get it to output an empty string or null at least?

 

Example:

Rows coming in: Name="Jason", Chair="Lazy",  Desk=""

JSON Output:

{ "Name":"Jason", "Chair":"Lazy", "Desk":[] }

 

5 REPLIES
Eight Stars

Re: twritejsonfield array when Null

I had the same problem.
Maybe tJSONOutput helps? Not sure...
I solved this differently, I think the answer is within your jsonpath-query.
like .Desk[0], you want .Desk ...
This website helps testing your query: http://www.jsonquerytool.com/
Five Stars

Re: twritejsonfield array when Null

twritejsonfield converts rows to JSON output. So it is not a JSON formatting issues since the JSON output component is the one doing the formatting.

 

After much google searching and community searching this is a bug or "feature" of the java class the component is using. I looked up that specific class and several people are complaining about this "feature" of the class as well. The fact that it treats nulls as arrays is completely nuts, but I guess that is what we have to deal with.

 

If you are encountering this issue your only solution is to find a way to modify the input or output of the twritejsonfield component to make sure the component either knows they are strings going in or modify them as string coming out.

Eight Stars

Re: twritejsonfield array when Null

Im not sure if I agree ... I think its not a bug
I think the json component is lacking the possibility to set a default value like an empty string "" or 0 for integers or: true / false. If it changes "" into [] that would be nuts...

The jsonpath / query component needs to handle empty arrays correctly.
This works perfectly fine and valid.
{
"key" : "value",
"array" : [],
"newarray" : [{"foo" : true}],
"foo" : ""
}
Five Stars

Re: twritejsonfield array when Null

It may sound nuts, but it is exactly what it does. Multiple people have verified that. Try not to get hung up on what works on the output. My REST call is expecting a string and it is getting an array, it is simply that.

 

If you send a row with of column value of  "" into the twritejsonfield it comes out the other side like this: {column: []}.

 

If I sent a space " " into the twritejsonfield it comes out the other side like this: {column: " "}.

 

If I sent a null meaning nothing into the twritejsonfield it comes out the other side like this: {column: []}.

 

If I sent a 0 into the twritejsonfield it comes out the other side like this: {column: 0}.

 

If I sent a true into the twritejsonfield it comes out the other side like this: {column: true}.

 

If I sent a blank boolean into the twritejsonfield it comes out the other side like this: {column: []}.

 

This can be tested and produces the same result every time in Talend with this component.

Eight Stars

Re: twritejsonfield array when Null

Absolutely nuts, totally agree!
They should implement it based on JSON.org specs.
It's a bug and not a 'feature' based on your testing and example, that proofs your point.
"" should be untouched.

For the sake of argument, JSON parsing should be able to handle this properly:
- values like [], null should be able to be parsed correctly.
You cant go wrong, empty is empty doesnt matter what type it is.

I've encountered [] this many times while receiving json response where a string value is set to [] in stead of null.

Thanks for testing and sharing !!!