Json Path查询没有数组

时间:2018-03-16 14:56:19

标签: c# json jsonpath json-query

在下面的JSON中我试图找到一个条件。只有当OrderNumber不为空或null时我才想要结果。

我尝试过的条件。但是没有为我工作的是:

> $..Item[?(@.OrderNumber)]
> $..Item[?(@.CurrentOrder.OrderNumber)]

任何建议将不胜感激。 我在这里测试我的查询https://jsonpath.curiousconcept.com/

{
        "Response": {
            "ID": "123456",
             "Items": {
                "Item": [
                    {                       
                        "CurrentOrder": {
                            "OrderNumber": "123",
                            "Status": ""
                        }
                    }
                ]
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

$..Item[?(@.CurrentOrder.OrderNumber)]是适用于您案例的正确JSONPath。

您使用的工具已知无法正常工作。请参阅示例this question

我使用JSONPath Online Evaluator和JSON

对上述查询进行了测试
{
    "Response": {
        "ID": "123456",
        "Items": {
            "Item": [
                {
                    "CurrentOrder": {
                        "OrderNumber": "123",
                        "Status": ""
                    }
                },
                {
                    "CurrentOrder": {
                        "OrderNumber": "456",
                        "Status": ""
                    }
                },
                {
                    "CurrentOrder": {
                        "Status": ""
                    }
                }
            ]
        }
    }
}

它给出了正确的结果:

[
  {
    "CurrentOrder": {
      "OrderNumber": "123",
      "Status": ""
    }
  },
  {
    "CurrentOrder": {
      "OrderNumber": "456",
      "Status": ""
    }
  }
]