Skip to content

Conditional output of object only if a specified property is present in source #279

Answered by larsga
digitalfiends asked this question in Q&A
Discussion options

You must be logged in to vote

Here's your JSLT simplified and indented, with parens added around the if statement, so you can see what effect it has:

{
  "Client": {
    ( if(.RC-INPUT-DTL-REC.RC-ACCNT-NBR) "ClientIdentification" ) : ...,
    "PersonSINIdentification": {
      "IdentificationID": string(.RC-INPUT-DTL-REC.RC-SIN-TTN)
  }
}

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 is null and the value is an object, and you've produced illegal JSON.

Rewrite to the following, and it should work:

{
  "Client": {
    "ClientIdentification" : if(.RC-INPUT-DTL-REC.RC-ACCNT-NBR) { ..…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@digitalfiends
Comment options

Answer selected by digitalfiends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
support Users asking how to solve a specific issue
2 participants