Handling Multiple 'null values

I am currently having this requirement where i need to handle all the null values coming in input. The common thing i noticed that all these values end with ‘_DATE’ but this is coming in an object. for all the null values, i need to pass value ‘0000-00-00’. The challenge which i am facing is this is dynamic. sometimes there are overall 28 fields ending with ‘_DATE’ and sometimes its more than 100.

[
  {
  "DEPRECIATIONAREAS": [
      {
        "ASSET": "00005000xxxx",
        "DEP_UNITS": 0,
        "O_START_DATE": "2006-09-01",
        "S_START_DATE": null,
        "INTEREST_START_DATE": null,
        "READINESS": "2006-08-31",
        "INDEX": "",
        "AGE_INDEX": "",
        "VAR_DEP_PORTION": 0.0000,
        "SCRAPVALUE": 0.0000,
		}
		]
	"POSTINGINFORMATION": [
      {
        "ASSET": "000050009142",
        "SUBNUMBER": "0000",
        "CAP_DATE": "2006-08-31",
        "DEACT_DATE": null,
        "INITIAL_ACQ": "2006-08-31",
        "INITIAL_ACQ_YR": 2006,
        "INITIAL_ACQ_PRD": 11,
        "PLRET_DATE": null,
        "PO_DATE": null,
        "CAP_KEY": ""
		}
		]
		}
		]

Can someone provide me a solution for this?

Thanks in Advance

Hey @aditya.gupta41 ,

The JSON you provided is not valid. Are these two separate samples just posted as one? (DEPRECIATIONAREAS,POSTINGINFORMATION)

Hi @aditya.gupta41,

Try with the following expression:

{}.extend(...$.entries().map(val => {[val[0]]: val[1].map(v => {}.extend(...v.entries().map(date => date[0].contains("_DATE") && date[1] == null ? {[date[0]]: "0000-00-00"} : {[date[0]]: date[1]} )))}))

This is assuming you have one or multiple arrays in the root object, assuming the arrays have one or multiple objects. This will iterate over each array find the “_DATE” key/value pair, and if a “_DATE” happens to be null, it will change the original value with the “0000-00-00”, and put back everything into the original format.

1 Like

These are arrays coming inside the data. I cannot post the actual output, but here is another example:

[
  {
    "data": {
      "TIMEDEPENDENTDATA": [
        {
          "ASSET": "000050",
          "SUBNUMBER": "0000",
          "FROM_DATE": "1900-01-01",
          "TO_DATE": "9999-12-31",
          "W_DATE": null,
          "X_DATE": null
        }
      ],
      "SELECTIONCRITERIA": [
        {
          "PARAMETER": "GENERALDATA",
          "FIELD": "ASSET",
          "SIGN": "I",
          "OPTION": "E",
          "LOW": "00005000",
          "HIGH": ""
        }
      ],
      "REALESTATE": [

      ],
      "POSTINGINFORMATION": [
        {
          "ASSET": "0000502",
          "SUBNUMBER": "0000",
          "C_DATE": "2006-08-31",
          "DEACT_DATE": null,
          "PLRET_DATE": null,
          "PO_DATE": null,
          "CAP_KEY": ""
        }
      ],
        }
      ],
      "NETWORTHVAL": [

      ],
      "INVENTORY": [
        {
          "ASSET": "000050",
          "SUBNUMBER": "0000",
          "V_DATE": null,
          "W_DATE": null
        }
      ],
      "INSURANCE": [

      ]
}
]

This didn’t worked for me

Failure: Map type does not have a method named: map, found in: …]: date[1]} ))). Perhaps you meant: mapKeys, mapValues, Resolution: Please check expression syntax and data types.

It didn’t because the first and second data source that you posted are completely different.
I updated my expression ( the only issue is that with the previous source, the data was on the root level, but on the new source, the data is within the “data” object ), please refer to the expression below:

{}.extend(...$data.entries().map(val => {[val[0]]: val[1].map(v => {}.extend(...v.entries().map(date => date[0].contains("_DATE") && date[1] == null ? {[date[0]]: "0000-00-00"} : {[date[0]]: date[1]} )))}))

Here is the result:

[
   {
      "data":{
         "TIMEDEPENDENTDATA":[
            {
               "ASSET":"000050",
               "SUBNUMBER":"0000",
               "FROM_DATE":"1900-01-01",
               "TO_DATE":"9999-12-31",
               "W_DATE":"0000-00-00",
               "X_DATE":"0000-00-00"
            }
         ],
         "SELECTIONCRITERIA":[
            {
               "PARAMETER":"GENERALDATA",
               "FIELD":"ASSET",
               "SIGN":"I",
               "OPTION":"E",
               "LOW":"00005000",
               "HIGH":""
            }
         ],
         "REALESTATE":[
            
         ],
         "POSTINGINFORMATION":[
            {
               "ASSET":"0000502",
               "SUBNUMBER":"0000",
               "C_DATE":"2006-08-31",
               "DEACT_DATE":"0000-00-00",
               "PLRET_DATE":"0000-00-00",
               "PO_DATE":"0000-00-00",
               "CAP_KEY":""
            }
         ],
         "NETWORTHVAL":[
            
         ],
         "INVENTORY":[
            {
               "ASSET":"000050",
               "SUBNUMBER":"0000",
               "V_DATE":"0000-00-00",
               "W_DATE":"0000-00-00"
            }
         ],
         "INSURANCE":[
            
         ]
      }
   }
]

Hi,

This still shows error:

Also, the $data is an object and inside that there are multiple arrays.

Try this:

$data.mapValues((v,k)=>v.map(x=>x.mapValues((v1,k1)=>k1.endsWith("_DATE") && v1 == null ? "0000-00-00" : v1 )))

This too is getting error:

failure:

“Map type does not have a method named: map, found in: …-00-00” : v1 )). Perhaps you meant: mapKeys, mapValues"

Well, that means that the fields TIMEDEPENDENTDATA,SELECTIONCRITERIA, etc. are objects, not arrays like in the sample you sent.

I think the data that you are sharing and the data that you are working with are completely different. I suggest you to share the actual data that you are working with and if there is some sensitive information just replace it with “X” or something else, because what we need is the actual format of the data.

Here is the json file. This is the actual output format.
mapping_SAP output output0.zip (1.6 KB)

Okay, I can see that the last property is an object not an array, that’s why it is failing. I am referring to “EH_RETURN” in your data. Removing that object, the expression works. I will update my expression and give you a solution where it checks whether the property is an object or array, this was unexpected.

1 Like

Hi @aditya.gupta41

I updated the expression, please refer to the following one:

{}.extend(...$data.entries().map(val => typeof val[1] == "object" ? ({val[0]]: val[1].mapValues((v, k) => k.contains("_DATE") && v == null ? "0000-00-00" : v})) : {[val[0]]: val[1].map(v => {}.extend(...v.entries().map(date => date[0].contains("_DATE") && date[1] == null ? {[date[0]]: "0000-00-00"} : {[date[0]]: date[1]} )))}))

Or this one ( with match operator ):

{}.extend(...$data.entries().map(val =>
	match typeof val[1] {
		"object" =>
				{
					[val[0]]: val[1].mapValues((v, k) => k.contains("_DATE") && v == null ? "0000-00-00" : v)
				},
		"array" =>
				{
					[val[0]]: val[1].map(v => {}.extend(...v.entries().map(date => date[0].contains("_DATE") && date[1] == null ? {[date[0]]: "0000-00-00"} : {[date[0]]: date[1]} )))
				}
	}
))
1 Like

This works. Thanks for your help @j.angelevski

1 Like