skip to Main Content

I have the following input JSON value :

{
  "A": {
    "B": [
      {
        "tempName": "rq3ewas",
        "name": "test1"
      },
      {
        "tempName": "",
        "name": "test2"
      },
      {
        "tempName": null,
        "name": "test3"
      },
      {
        "tempName": "test4"
      }
    ]
  }
}

and want to get the following output based on ;

scenario1: If "tempname" is not null, then set the value of "name" to "tempName".

scenario2: If "tempname" is null, then keep the default value of "name" without changing it.

scenario3: If "tempname" is empty string(""), then keep the default value of "name" without changing it

scenario4: But If "tempname" is not present, then keep the default value of "name"

Except first scenario, last three are not working. If the code is modified last three works and first scenario is getting failed.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : null
    }, {
      "name" : ""
    }, {
      "name" : "test4"
    } ]
  }
}

Expected O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : "test2"
    }, {
      "name" : "test3"
    }, {
      "name" : "test4"
    } ]
  }
}
[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

2

Answers


  1. Chosen as BEST ANSWER

    Thank you, This is working. Facing issue with one more spec similar for converting JSON to Array.

    Input JSON:

    [
      {
        "bI": {
          "id": "t1",
          "locale": [
            "id_ID",
            "id_ID"
          ],
          "tempName": "",
          "name": "Test1",
          "isActive": false,
          "pT": "Re"
        },
        "id": "t1",
        "ContextID": "id_ID",
        "cf": {
          "name": "asd"
        },
        "dE": {
          "sS": "arte"
        },
        "mk": {},
        "pd": {}
      }
    ]
    

    spec applied:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&",
            "bI": {
              "*": "bI.&",
              "tempName": {
                "": "&2.&1",
                "*": {
                  "@1": "&3.&2"
                }
              }
            }
          }
        }
      }
    ]
    

    O/p:

    {
      "bI" : {
        "id" : "t1",
        "locale" : [ "id_ID", "id_ID" ],
        "tempName" : null,
        "name" : "Test1",
        "isActive" : false,
        "pT" : "Re"
      },
      "id" : "t1",
      "ContextID" : "id_ID",
      "cf" : {
        "name" : "asd"
      },
      "dE" : {
        "sS" : "arte"
      },
      "mk" : { },
      "pd" : { }
    }
    

    Expected output:

    [
    {
      "bI" : {
        "id" : "t1",
        "locale" : [ "id_ID", "id_ID" ],
        "tempName" : null,
        "name" : "Test1",
        "isActive" : false,
        "pT" : "Re"
      },
      "id" : "t1",
      "ContextID" : "id_ID",
      "cf" : {
        "name" : "asd"
      },
      "dE" : {
        "sS" : "arte"
      },
      "mk" : { },
      "pd" : { }
    }
    ]
    

  2. Principally you should get rid of the empty string, since manipulating null valued attributes is possible with modify specs. Having aimed this, the following transformation might be used :

    [
      { //concert "" valued "tempName" attribute's value to null,
        //while removing the null valued ones(doesn't matter)
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "tempName": {
                  "": "&4.&3[&2].&1", //convert "" valued "tempName" attribute to to the one with null value
                  "*": {
                    "@1": "&5.&4[&3].&2"
                  }
                },
                "*": "&3.&2[&1].&"
              }
            }
          }
        }
      },
      { //set "name" attributes by the values of the 
        //"tempName" attributes provided that they're non-null  
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
                "name": "=notNull(@(1,tempName))"
              }
            }
          }
        }
      },
      { //keep the "name" attributes only
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "*": {
                "tempName": ""
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search