Conditional output of object only if a specified property is present in source #279
-
First off, just wanted to say a quick thanks for this fantastic project. I just recently discovered it, so I'm still learning what it is capable of. Unfortunately, I've hit an issue that I can't seem to resolve and don't see an obvious solution in the tutorial/functions readmes or in the discussions here. Issue:I need a JSON object to be created only if a source key/value is present. Simplified source JSON:
Expected output JSON (when RC-ACCNT-NBR is present in source)
Expected output JSON (when RC-ACCNT-NBR is not present in source)
In this case, the ClientIdentification object should not be written to the output. JSLT for mapping:This is what I have for the mapping (extra stuff removed to keep things clear):
Result:When RC-ACCNT-NBR is present in the source JSON, everything works well and I get the expected JSON output. When the RC-ACCNT-NBR is not present in the source, I get the following exception: com.schibsted.spt.data.jslt.JsltException: Object key must be string at :3:9 Is it possible to achieve this with JSLT? I would assume so and that it is just my lack of experience with the syntax. Any help would be greatly appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Here's your JSLT simplified and indented, with parens added around the
In other words, your Rewrite to the following, and it should work:
|
Beta Was this translation helpful? Give feedback.
Here's your JSLT simplified and indented, with parens added around the
if
statement, so you can see what effect it has:In other words, your
if
affects the"ClientIdentification"
key, but not the value, which is always present. So when the"RC-INPUT-DTL-REC.RC-ACCNT-NBR"
key is not in the input, the key isnull
and the value is an object, and you've produced illegal JSON.Rewrite to the following, and it should work: