首页 > 解决方案 > 如何在 JSONPath 中有条件地返回特定值?

问题描述

我有一个像这样的对象:

{
    "content": {
        "iteration_size": 1
    }
}

我需要一个 JSONPath,null如果 的值next_item正好为 1,则返回,iteration_size否则返回值。所以上面应该返回null并且"iteration_size": 2应该返回 2。这可能在 JSONPath 中吗?

我尝试过 的变体$.content[?(@.iteration_size == 1)].iteration_size,但我尝试过的两位在线评估员甚至不同意我几次尝试的结果。

用例是 AWS Batch API,其中数组大小必须是null2 到 10,000 之间的一个或一个数字。

标签: jsonjsonpath

解决方案


AWS Support 建议这是不可能的,我应该在 step 函数中使用一个分支。我最终替换了这个:

.next(batch_submit_job)
[…]

有了这个:

.next(
    aws_stepfunctions.Choice(self, "check_maybe_array")
    .when(
        aws_stepfunctions.Condition.number_equals("$.content.iteration_size", 1),
        single_batch_submit_job,
    )
    .otherwise(array_batch_submit_job)
    .afterwards()
)

其中只有array_batch_submit_job一个array_size. 请注意 step 函数的其余部分如何最终包裹最后一次next()调用中而不是顶层。


推荐阅读