JSONPath从表中的映射获取值

huangapple go评论71阅读模式
英文:

JSONPath get value from map within a table

问题

{
   "data":{
      "fruit":[
         {
            "role":[
               {
                  "role":"passive"
               }
            ],
            "objectType":"apple"
         },
         {
            "role":[
               {
                  "role":"active"
               }
            ],
            "objectType":"orange"
         }
      ]
   }
}

我想要在 roleactive 的情况下获取 objectType 的值。在这个例子中,结果将是 orange。我已经想出了以下代码,但是它没有返回任何内容。我该如何修复这个问题?

$.data.fruit[?(@.role[0].role == 'active')].objectType

请注意,fruit 数组中的元素数量可能会变化,并且不能保证最后一个元素是正确的。

英文:

I have a JSON string as follows (simplified):

{
   "data":{
      "fruit":[
         {
            "role":[
               {
                  "role":"passive"
               }
            ],
            "objectType":"apple"
         },
         {
            "role":[
               {
                  "role":"active"
               }
            ],
            "objectType":"orange"
         }
      ]
   }
}

I would like to get objectType value where role is active. In this example the result would be orange. I've come up with the following code, however it doesn't return anything. How do I fix this?

$.data.fruit[?(@.role.role == 'active')]

Note that the number of elements in fruit array may vary and there is no guarantee that the last element would be correct.

答案1

得分: 0

根据OP的要求,这是最终的最终答案!

$.data.fruit[?(@..role..role中存在'active')].objectType
英文:

At OP's request, here's the final, final answer!

$.data.fruit[?('active' in @..role..role)].objectType

答案2

得分: -2

在这种情况下,如果它是一个数字,在字符串化的每个递进级别将缩进这么多空格字符(最多10个)。

如果它是一个字符串,递进级别将被这个字符串缩进(或其前面的十个字符)。

英文:

On the off chance that it is a number, progressive levels in the stringification will each be indented by this many space characters (up to 10).

In the event that it is a string, progressive levels will be indented by this string (or its initial ten characters).

huangapple
  • 本文由 发表于 2020年7月27日 16:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定