0

INPUT:

 { 
   "itemName": null,
   "itemNumber": 22
 }

OUTPUT:

 {
   "isItemNameNull": true,
   "itemNumber": 22
 }

I should not map the value if its null but map 'isItemNameNull' to true,

SPEC:

[
 {
"operation": "shift",
"spec": {
  "itemName": {
    "null": {
      "true": "isItemNameNull"
    },
    "": {
      "#TRASH": "TRASH"
    },
    "*": {
      "@(2,itemName)": "itemName"
    }
  },
  "itemNumber": {
    "null": {
      "#TRASH": "TRASH"
    },
    "": {
      "#TRASH": "TRASH"
    },
    "*": {
      "@(2,itemNumber)": "itemNumber"
    }
  }
  }
 }
]

I mapped the flag but it did'nt took it from null condition.Can any one please suggest me a help.Thanks.

tender
  • 3
  • 1
  • 4

3 Answers3

2

You can achieve this by a combination of default and shift operations.

First use a default to detect if itemName is null and assign it a default value of true

Then use a shift to process the itemName appropriately:

  • if itemName is true (i.e. was set in the default because its original value was null), then set isItemNameNull using the value of itemName
  • else copy itemName to the output (I've assumed that you want to do this, otherwise just leave this second bit out)
[
  {
    "operation": "default",
    "spec": {
      "itemName": true
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "itemName": {
        "true": {
          "@1": "isItemNameNull"
        },
        "*": {
          "@1": "itemName"
        }
      }
    }
  }
]

One caveat being that if the input has a string of "true" for itemName

{
  "itemName": "true",
  "itemNumber": 22
}
  

then the output will be

{
  "isItemNameNull": "true",
  "itemNumber": 22
}

So not foolproof!

To get around that pitfall you could use a 3 step approach:

  • if itemName is null assign it an appropriate default "marker" value
  • process itemName and if it has the "marker" value set isItemNameNull to String value "true"
  • convert isItemNameNull to a boolean
[
  {
    "operation": "default",
    "spec": {
      "itemName": "null_marker"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "itemName": {
        "null_marker": {
          "#true" :"isItemNameNull"
        },
        "*": {
          "@1": "itemName"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "isItemNameNull": "=toBoolean"
    }
  }
]
Malcatron
  • 21
  • 4
0

I tried this case. I edited a little in your input json also.

Input Json
----------
{
  "itemName": "null",
  "itemNumber": 22
}


SPEC:
----
    [
      {
        "operation": "shift",
        "spec": {
          "itemName": {
            "null": {
              "#true": "isItemNameNull"
            }
          },
          "itemNumber": "itemNumber"
        }
       }
    ]

Output Json
-----------

{
  "isItemNameNull" : "true",
  "itemNumber" : 22
}

Try yourself please

-1

There isn't a good way of doing that.

Milo S
  • 4,466
  • 1
  • 19
  • 22