空手道 - 将json字符串字段转换为json对象

发布于 2025-02-01 19:48:24 字数 667 浏览 4 评论 0原文

我有一个API返回一个JSON数组,其中一个字段包含逃脱的JSON。假设看起来那样:

{
  "results":[
    {
      "id": "id-1",
      "data": "{\"name\", \"Adam\", \"surname\": \"Parker\"}"
    },
    {
      "id": "id-2",
      "data": "{\"name\", \"Adam\", \"surname\": \"Parker-Bates\"}"
    },
    {
      "id": "id-3",
      "data": "{\"name\", \"Adam Robert\", \"surname\": \"Parker\"}"
    }
  ]
}

现在,我需要断言该数组完全包含一个匹配给定标准的一个元素,假设我正在寻找Adam Parker。如果将其作为合适的JSON对象返回,它将非常简单。我可以搜索子字符串,但它可以给我错误的结果。也许我可以寻找某些正则表达式仅匹配\“ Adam \”“ \ Parker \”,但宁愿将其转换为JSON对象,因为真实情况方案具有更复杂的嵌套数据。有什么方法可以与空手道一起做?我不需要ID,因此,如果这使它变得更容易,我可以将数据提取到字符串数组。

I have an API that returns an JSON array of objects, where one of fields contains escaped JSON. Let's say it looks like that:

{
  "results":[
    {
      "id": "id-1",
      "data": "{\"name\", \"Adam\", \"surname\": \"Parker\"}"
    },
    {
      "id": "id-2",
      "data": "{\"name\", \"Adam\", \"surname\": \"Parker-Bates\"}"
    },
    {
      "id": "id-3",
      "data": "{\"name\", \"Adam Robert\", \"surname\": \"Parker\"}"
    }
  ]
}

Now, I need to assert that that array contains exactly one element matching given criteria, let's say I'm looking for Adam Parker. If it would be returned as a proper JSON object it would be pretty straightforward. I could search for substrings, but it can give me false results. Maybe I could look for certain regular expressions to match only \"Adam\" and "\Parker\" but would prefer to convert that strings to JSON objects, since real case scenario has much more complex, nested data. Is there any way to do it with karate? I do not need ids, so I can extract data to array of strings if that would make it easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-02-08 19:48:24

很容易,空手道有一些助手功能悬挂在空手道 js对象: https://github.com/karatelabs/karate#the-karate-object

按照您的JSON不正确的方式(逗号而不是Colon),这使我坚持了一段时间。

这是一个演示,还请参考有关如何进行JSON变换的文档,我们在下面使用JS Array方法: https://github.com/karatelabs/karate#json-transforms

* def response =
"""
{
  "results":[
    {
      "id": "id-1",
      "data": "{\"name\": \"Adam\", \"surname\": \"Parker\"}"
    },
    {
      "id": "id-2",
      "data": "{\"name\": \"Adam\", \"surname\": \"Parker-Bates\"}"
    },
    {
      "id": "id-3",
      "data": "{\"name\": \"Adam Robert\", \"surname\": \"Parker\"}"
    }
  ]
}
"""
* def names = response.results.map(x => karate.fromString(x.data))
* match names contains { name: 'Adam', surname: 'Parker' }

That's easy, Karate has a few helper functions hanging off the karate JS object: https://github.com/karatelabs/karate#the-karate-object

By the way your JSON is not well-formed (comma instead of colon) which got me stuck for a while.

Here's a demo, also refer the documentation on how to do JSON transforms, we are using JS array methods below: https://github.com/karatelabs/karate#json-transforms

* def response =
"""
{
  "results":[
    {
      "id": "id-1",
      "data": "{\"name\": \"Adam\", \"surname\": \"Parker\"}"
    },
    {
      "id": "id-2",
      "data": "{\"name\": \"Adam\", \"surname\": \"Parker-Bates\"}"
    },
    {
      "id": "id-3",
      "data": "{\"name\": \"Adam Robert\", \"surname\": \"Parker\"}"
    }
  ]
}
"""
* def names = response.results.map(x => karate.fromString(x.data))
* match names contains { name: 'Adam', surname: 'Parker' }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文