最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jolt - Issue with JSON Transformation spec - Stack Overflow

programmeradmin3浏览0评论

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:

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

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:

[
  {
    "Validator": [
      {
        "name": "API_Automation_Client",
        "guid": "8013091c-6f2e-46c4-b406-b11a4b78c628",
        "status": "PUBLISHED"
      },
      {
        "name": "HNCServerTest",
        "guid": "8a73c938-8687-4de2-9020-0a5bed37f91d",
        "status": "NOT PUBLISHED"
      },
      {
        "name": "Time",
        "guid": "Time",
        "status": "PUBLISHED"
      },
      {
        "name": "SV-p1",
        "guid": "aa2a1f6a-165b-409b-978e-ee436d0ae81c",
        "status": "PUBLISHED"
      }
    ],
    "selectedRelease": {},
    "Replay": [],
    "entity": "Validator",
    "publishedString": "SV-p1,Time,API_Automation_Client"
  }
]
Share Improve this question edited Jan 19 at 8:20 Barbaros Özhan 65.4k11 gold badges36 silver badges61 bronze badges asked Jan 17 at 19:39 Alisha SharanAlisha Sharan 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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"
      }
    }
  }
]

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"
          }
        }
      }
    }
  }
]
发布评论

评论列表(0)

  1. 暂无评论