skip to Main Content

I have below input json:

[
  {
    "Validator": [
      {
        "name": "API_Automation_Client",
        "guid": "8013091c-6f2e-46c4-b406-b11a4b78c628",
        "status": "NOT PUBLISHED"
      },
      {
        "name": "HNCServerTest",
        "guid": "8a73c938-8687-4de2-9020-0a5bed37f91d",
        "status": "NOT PUBLISHED"
      },
      {
        "name": "Time",
        "guid": "Time",
        "status": "NOT PUBLISHED"
      },
      {
        "name": "SV-p1",
        "guid": "aa2a1f6a-165b-409b-978e-ee436d0ae81c",
        "status": "NOT PUBLISHED"
      }
    ],
    "selectedRelease": {},
    "Replay": [],
    "entity": "Validator",
    "publishedString": "SV-p1,Time,API_Automation_Client"
  }
]

I am using json spec to convert it such that the status is "PUBLISHED" if the Validator name is present in publishedString otherwise the status remains as NOT PUBLISHED.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Validator": {
          "*": {
            "name": "[0].Validator[&1].name",
            "guid": "[0].Validator[&1].guid",
            "status": "[0].Validator[&1].status"
          }
        },
        "selectedRelease": "[0].selectedRelease",
        "Replay": "[0].Replay",
        "entity": "[0].entity",
        "publishedString": "[0].publishedString"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Validator": {
          "*": {
            "status": "=split(@(2,publishedString), ',').contains(@(0,name)) ? 'PUBLISHED' : 'NOT PUBLISHED'"
          }
        }
      }
    }
  }
]

But it is giving status as NOT PUBLISHED for all Validators.

Please suggest.

Below is the output I am expecting:

enter image description here

2

Answers


  1. If you split the "publishedString" by the value of the "name", you will get a list of size 2 or 1 according to the presence or absence of the "name" in the string, except if the "name" value is the latest, so I use the trick to add a dummy element at the end. Then it’s just a matter to generated the "status" according to the value of the size.

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "s": "=concat(@(3,publishedString),',noop')",
                "statuses": "=split(@(1,name),@(1,s))",
                "test": "=size(@(1,statuses))"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "name": "[0].Validator[&1].name",
                "guid": "[0].Validator[&1].guid",
                "test": {
                  "1": {
                    "#NOT_PUBLISHED": "[0].Validator[&3].status"
                  },
                  "2": {
                    "#PUBLISHED": "[0].Validator[&3].status"
                  }
                }
              }
            },
            "selectedRelease": "[0].selectedRelease",
            "Replay": "[0].Replay",
            "entity": "[0].entity",
            "publishedString": "[0].publishedString"
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can use the following transformations :

    [
      {//get an array from the comma-separated substrings of publishedString 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "nameS": "=split(',',@(3,publishedString))"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "nameS": {
                  "*": "[&4].&3[&2].&1.@0.@2,name"
                },
                "*": "[&3].&2[&1].&"
              }
            },
            "*": "[&1].&"
          }
        }
      },
      {//mark the attributes as PUBLISHED provided they match with the nester objects' keys 
        "operation": "shift",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "nameS": {
                  "*": {
                    "&": { "#PUBLISHED": "[&6].&5[&4].status" }
                  }
                },
                "*": "[&3].&2[&1].&"
              }
            },
            "*": "[&1].&"
          }
        }
      },
      {//get rid of the duplictes of the statuses
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "Validator": {
              "*": {
                "status": "=lastElement"
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search