从REST API我收到下一个JSON:

[
   {
      "id":3130326,
      "content":{
         "icon_256x256":{
            "flags":[
               
            ],
            "id":26864619,
            "type":"static"
         },
         "image_1080x607":{
            "flags":[
               
            ],
            "id":26864707,
            "type":"static"
         }
      },
      "status":"blocked",
      "textblocks":{
         "cta_sites_full":{
            "text":"learnMore",
            "title":""
         },
         "text_220":{
            "text":"Domain",
            "title":""
         },
         "text_90":{
            "text":"Don't worry, be happy!",
            "title":"Test!"
         }
      },
      "urls":{
         "primary":{
            "id":90970124,
            "preview_link":"",
            "url_types":[
               "external_new",
               "external"
            ]
         },
         "url_slide_1":{
            "id":90970124,
            "preview_link":"",
            "url_object_type":"domain"
         }
      },
      "user_can_request_remoderation":false
   }
]

我想从下面1级的content个对象中抓取:在这种情况下,它是icon_256x256image_1080x607,抓取场id,并保持不变,但只需要id个场.此外,我需要字段textblocks和内部urls做同样的事情与content.因此,我希望得到这样的结果:

{
   "content":{
      "icon_256x256":{
         "id":26864619
      },
      "image_1080x607":{
         "id":26864707
      }
   },
   "status":"blocked",
   "textblocks":{
      "cta_sites_full":{
         "text":"learnMore",
         "title":""
      },
      "text_220":{
         "text":"Domain",
         "title":""
      },
      "text_90":{
         "text":"Don't worry, be happy!",
         "title":"Test!"
      }
   },
   "urls":{
      "primary":{
         "id":90970124
      },
      "url_slide_1":{
         "id":90970124
      }
   }
}

content中的对象可能有不同的名称,比如video_landscape而不是icon_256x256,所以我不能基于名称进行循环.但它总是比你低一层,你可以看到.urls场也一样.我希望我能描述清楚!

我的try 是,但它不工作(:

[
  {
    "operation": "shift",
    "spec": {
      "content": {
        "*": {
          "id": "content.&1.id"
        }
      },
      "textblocks": {
        "@": "textblocks.&"
      },
      "urls": {
        "primary": {
          "id": "urls.primary.id"
        }
      }
    }
  }
]

推荐答案

您可以使用以下shift个变换规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "content|urls": {
          "*": {
            "id": "&2.&1.id" // &2 represents going 2 levels up in order to grab
                             // the keys content and urls
                             // and &1 stands for getting the respective subkeys 
                             // of them from just one level up
          }
        },
        "status|textblocks": "&" // replicate the values of these attributes respectively
      }
    }
  }
]

Json相关问答推荐

Jolt转换,如果任何字段为空,则将对象值设置为空

合并二维数组的Jolt表达式

在Snowflake中查询JSON时,属性名称是否支持绑定参数?

为什么 Django Rest API 序列化器没有正确序列化多对多字段

JOLT 转换仅过滤一个字段

如何在不使用 Newtonsoft.JSON 的情况下序列化/反序列化

将哈希表转换为 json 后,Powershell 缺少数组

如何判断 Json 对象中是否存在键并获取其值

如何找出实际安装了哪个版本的 bower 包?

将 ES6 类对象序列化为 JSON

在 JSON API Wordpress 上启用 CORS

将json字符反序列化为枚举

jQuery AJAX 和 JSON 格式

使用 GSON 解析嵌套的 JSON 数据

在 Qt 4.7 中解析 JSON 的最简单方法

有没有办法折叠 Postman 中的所有 json 字段

使用 JSON.NET 序列化/反序列化对象字典

无法解析 JSON 文件中的 TAB

JSON键是否需要唯一?

在没有 ASP.NET Core 的情况下将 IConfigurationSection 绑定到复杂对象