更换空手道中的JSON KEY

发布于 2025-01-31 11:56:39 字数 459 浏览 0 评论 0原文

我需要将JSON发送到端点,但是我需要用变量替换密钥。

我有这个代码

 .....  

  * def idJson = response.id

  Given path <mypath>
  And headers {Authorization: '#(auth)'}
  And request read('classpath:myjson.json')
  .....

myjson.json文件是

{ “一个”: { ... “ b”:false, “ C”:是的 },, “ D”: { '#(idjson)': { “钥匙”:[ ..... ..... 这是给出的 } } }

但是,该值在JSON中没有替换。当我执行请求时,我会看到JSON包含字符串“#(IDJSON)”,而不是变量的值。关于如何解决这个问题吗?

谢谢

I need to send a Json to an endpoint but I need to replace a key with a variable.

I've this code

 .....  

  * def idJson = response.id

  Given path <mypath>
  And headers {Authorization: '#(auth)'}
  And request read('classpath:myjson.json')
  .....

The myjson.json file is

{
"a":
{
...
"b":false,
"c":true
},
"d":
{
'#(idJson)':
{
"Key":[
.....
]
}
}
}

But the value is not replace in the json. When I perform the request I see that the json contains the string '#(idJson)' instead of the value of the variable. Any idea about how to solve this?

Thank you

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

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

发布评论

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

评论(1

硪扪都還晓 2025-02-07 11:56:39

嵌入式表达式不能用于修改JSON密钥。您必须使用这样的JS操作:

* def idJson = 'foo'
* def body = {}
* body[idJson] = 'bar'
* url 'https://httpbin.org/anything'
* request body
* method post
* status 200
* match response.json == { foo: 'bar' }

请注意,d可以这样替换,因此您仍然可以读取文件并保持动态:

{ "d": "#(temp)" }

temp可以是JSON,在阅读文件之前,在您的功能中定义。

如果这太麻烦了,请贡献代码:)

Embedded expressions cannot be used to modify a JSON key. You have to use a JS operation like this:

* def idJson = 'foo'
* def body = {}
* body[idJson] = 'bar'
* url 'https://httpbin.org/anything'
* request body
* method post
* status 200
* match response.json == { foo: 'bar' }

Note that d could be replaced like this so you can still read the file and be dynamic:

{ "d": "#(temp)" }

And temp can be JSON that is defined in your feature before reading the file.

If this is too troublesome, please contribute code :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文