在JSON转换中将“替换为”

bvjveswy  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(146)

如何将\"替换为" JOLT json转换。

输入json

{
  "cpf": "[{\"property\":\"PQR\",\"value\":\"-\"},{\"property\":\"Exclude\",\"value\":\"-\"},{\"property\":\"Test\",\"value\":null},{\"property\":\"Test\",\"value\":null},{\"property\":\"Requested\",\"value\":\"PNT\"},{\"property\":\"Flag\",\"value\":\"TR\"},{\"property\":\"Name\",\"value\":\"Some Sentense\"},{\"property\":\"Length\",\"value\":null},{\"property\":\"ID\",\"value\":\"098789\"},{\"property\":\"BUS ID\",\"value\":\"123456988433\"}]"
}

应为json

{
  "cpf":[
    {
      "property":"PQR",
      "value":"-"
    },
    {
      "property":"Exclude",
      "value":"-"
    },
    {
      "property":"Test",
      "value":null
    },
    {
      "property":"Test",
      "value":null
    },
    {
      "property":"Requested",
      "value":"PNT"
    },
    {
      "property":"Flag",
      "value":"TR"
    },
    {
      "property":"Name",
      "value":"Some Sentense"
    },
    {
      "property":"Length",
      "value":null
    },
    {
      "property":"ID",
      "value":"098789"
    },
    {
      "property":"BUS ID",
      "value":"123456988433"
    }
  ]
}

我试过按照规格,但没有工作

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "cpf": "=split('\"',@(1,cpf))"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join('',@(1,cpf))"
    }
  }
]

我没有得到在第二个操作如何添加只有"没有转义字符

yzuktlbb

yzuktlbb1#

嗨,Prasad Jolt还不支持替换功能,我更喜欢在这个用例中使用Java,因为一个简单的字符串替换就可以解决这个问题。

String inputJson= ""{\n" +
                "  \"cpf\": \"[{\\\"property\\\":\\\"PQR\\\",\\\"value\\\":\\\"-\\\"},{\\\"property\\\":\\\"Exclude\\\",\\\"value\\\":\\\"-\\\"},{\\\"property\\\":\\\"Test\\\",\\\"value\\\":null},{\\\"property\\\":\\\"Test\\\",\\\"value\\\":null},{\\\"property\\\":\\\"Requested\\\",\\\"value\\\":\\\"PNT\\\"},{\\\"property\\\":\\\"Flag\\\",\\\"value\\\":\\\"TR\\\"},{\\\"property\\\":\\\"Name\\\",\\\"value\\\":\\\"Some Sentense\\\"},{\\\"property\\\":\\\"Length\\\",\\\"value\\\":null},{\\\"property\\\":\\\"ID\\\",\\\"value\\\":\\\"098789\\\"},{\\\"property\\\":\\\"BUS ID\\\",\\\"value\\\":\\\"123456988433\\\"}]\"\n" +
                "}"";

inputJson.replace("\\","").replace("]\"","]").replace("\"[","[")

这将生成预期的Json Array输出。

相关问题