使用JayWay JSONPath提取不同深度的多个JSON对象 [英] Extract several JSON objects at different depths with JayWay JSONPath

查看:366
本文介绍了使用JayWay JSONPath提取不同深度的多个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • I have a JSON
  • I'm trying to grab every element in an array that has some particular nested objects. The hard part is that some of these objects are nested at different depths.
  • I'm using JayWay JsonPath https://github.com/json-path/JsonPath, and my code works exactly like https://jsonpath.herokuapp.com

这是在我们的平台上使用的 https://dashdash.com -包含与已知网站集成的电子表格服务(以及您的私有API).

This is to use on our platform, https://dashdash.com - a spreadsheet with integrations for known web services (and your private APIs too).

考虑以下 source JSON ,我只想返回具有嵌套对象B,C和G的数组元素.G与B和C的深度不同.

Consider the following source JSON, I want to return only the array elements that have nested objects B, C and G. G is on a different depth than B and C.

在下面,您可以看到退货的来源和2个选项.

Below you can see the source and 2 options for the return.

源JSON

[  
   {  
      "A":"val1",
      "B":"val2",
      "C":"val3",
      "D":{  
         "E":[  
            {  
               "F":"val4"
            }
         ],
         "G":[  
            {  
               "H":"val5",
               "I":"val6",
               "J":"val7"
            }
         ]
      }
   },
   {  
      "A":"val8",
      "B":"val9",
      "C":"val10",
      "D":{  
         "E":[  
            {  
               "F":"val11"
            }
         ],
         "G":[  
            {  
               "H":"val12",
               "I":"val13",
               "J":"val14"
            }
         ]
      }
   },
   {  
      "A":"val15",
      "B":"val16"
   },
   {  
      "A":"val8",
      "B":"val9",
      "C":"val10",
      "D":{  
         "E":[  
            {  
               "F":"val11"
            }
         ]
      }
   }
]

预期回报选项1.

[
   {
      "B":"val2",
      "C":"val3",
      "G":[
         {
            "H":"val5",
            "I":"val6",
            "J":"val7"
         }
      ]
   },
   {
      "B":"val9",
      "C":"val10",
      "G":[
         {
            "H":"val12",
            "I":"val13",
            "J":"val14"
         }
      ]
   }
]

预期回报选项2.

[
   {
      "B":"val2",
      "C":"val3",
      "D":{
         "E":[
            {
               "F":"val4"
            }
         ],
         "G":[
            {
               "H":"val5",
               "I":"val6",
               "J":"val7"
            }
         ]
      }
   },
   {
      "B":"val9",
      "C":"val10",
      "D":{
         "E":[
            {
               "F":"val11"
            }
         ],
         "G":[
            {
               "H":"val12",
               "I":"val13",
               "J":"val14"
            }
         ]
      }
   }
]

我在哪里

  • 我可以使用查询$..['B','C','D']
  • 提取具有B,C和D的所有数组元素

    Where I am

    • I can extract all the array elements that have B,C and D, with the query $..['B','C','D']
    • 我尝试提取B,C和G,但以下所有查询均失败:

      I have tried to extract B, C and G, but all the following queries fail:

      • $..['B','C','G']:返回null.
      • $..['B','C',['D'].['G']]:仅返回G内部的对象.
      • $..['B','C','G']: returns null.
      • $..['B','C',['D'].['G']]: returns only the objects inside G.

      同样,我正在使用JayWay JsonPath https://github.com/json-path/JsonPath ,我的代码就像 https://jsonpath.herokuapp.com 一样工作.

      Again, I'm using JayWay JsonPath https://github.com/json-path/JsonPath, and my code works exactly like https://jsonpath.herokuapp.com.

      预先感谢

      推荐答案

      我一直在尝试一些不同的方法,但我认为更简单的表达式可以解决问题:

      I've been trying some different approaches and I think a simpler expression does the trick:

      $.*[?(@.B && @.C && @.D.G)]
      

      除默认设置外,此操作不需要任何特殊配置(根据在 https://jsonpath.herokuapp上进行的实验). com 并产生以下结果:

      This doesn't need any special config other than default (according to experiment done on https://jsonpath.herokuapp.com and yields the following result:

      [
         {
            "A" : "val1",
            "B" : "val2",
            "C" : "val3",
            "D" : {
               "E" : [
                  {
                     "F" : "val4"
                  }
               ],
               "G" : [
                  {
                     "H" : "val5",
                     "I" : "val6",
                     "J" : "val7"
                  }
               ]
            }
         },
         {
            "A" : "val8",
            "B" : "val9",
            "C" : "val10",
            "D" : {
               "E" : [
                  {
                     "F" : "val11"
                  }
               ],
               "G" : [
                  {
                     "H" : "val12",
                     "I" : "val13",
                     "J" : "val14"
                  }
               ]
            }
         }
      ]
      

      您怎么看?

      这篇关于使用JayWay JSONPath提取不同深度的多个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆