Json 如何删除Wiremock中每个循环中的最后一个逗号
我需要为一个请求编写一个存根,如下所示:Json 如何删除Wiremock中每个循环中的最后一个逗号,json,handlebars.js,wiremock,wiremock-standalone,Json,Handlebars.js,Wiremock,Wiremock Standalone,我需要为一个请求编写一个存根,如下所示: [ { "todo_id": 1 }, { "todo_id": 2 } ] "response": { "status": 200, "body": "[ {{#each (jsonPath request.body '$') as |todo|}} { \"to
[
{ "todo_id": 1 },
{ "todo_id": 2 }
]
"response": {
"status": 200,
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
{ \"todo_id\": {{todo.todo_id}} },
{{/each}}
]"
}
{{#eq todo {{jsonPath request.body '$.[-1]'}} }}
请求中todo对象的数量可能会有所不同
我目前的回答如下:
[
{ "todo_id": 1 },
{ "todo_id": 2 }
]
"response": {
"status": 200,
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
{ \"todo_id\": {{todo.todo_id}} },
{{/each}}
]"
}
{{#eq todo {{jsonPath request.body '$.[-1]'}} }}
请注意,我将正文隔开以使其更具可读性,在实际的存根中,它都在一行上
所以我的问题是,如果请求中传递了多个对象,我需要在todo对象后加逗号。但是,这将最后一个对象也保留逗号,因此如果发送了上述请求,则这将是响应:
[
{ "todo_id": 1 },
{ "todo_id": 2 },
]
最后一个逗号使Python应用程序中需要从该WireMock存根读取响应的.json()
方法失败
有没有想过如何去掉最后一个逗号?我在想可能在逗号周围有一个eq
条件,并检查当前todo
变量是否与{{jsonPath request.body'$.-1]}
相同,但这样写:
[
{ "todo_id": 1 },
{ "todo_id": 2 }
]
"response": {
"status": 200,
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
{ \"todo_id\": {{todo.todo_id}} },
{{/each}}
]"
}
{{#eq todo {{jsonPath request.body '$.[-1]'}} }}
也不管用
任何关于如何去掉最后一个逗号的建议都将不胜感激。谢谢:)在谷歌群组上找到了答案 可以在每个循环中使用
@last
来检测您何时在最后一项上,例如:
{{#each (jsonPath request.body '$.things') as |thing|}}
{{#if @last}}
{ "thing": {{{thing}}} }
{{else}}
{ "thing": {{{thing}}} },
{{/if}}
{{/each}}