使用 JQ 从对象中去除逗号重塑 JSON 文件

Reshape JSON File with JQ stripping commas from objects

我正在构建一个 ROKU 应用程序,并且正在重新格式化从 API 中提取的 json 文件,使其格式与 ROKU Direct Publisher 提要相同。

这就是 ROKU 所期待的...

{
    "providerName": "Acme Productions",
    "lastUpdated": "2015-11-11T22:21:37+00:00",
    "language": "en",
    "categories": [
        ...
    ],
    "playlists": [
        ...
    ],
    "movies": [
        ...
    ],
    "liveFeeds": [
        ...
    ],
    "series": [
        ...
    ],
    "shortFormVideos":  [
        ...
    ],
    "tvSpecials": [
        ...
    ]
}

我正在使用 jq 重塑它,但遇到了问题。

我当前的 json 文件基本上是这样的,并且一直在继续(我已经删除了大部分内容,因为键对我的要求并不重要...... )

{
"page_info":{
    "total_results":1000,
    "results_per_page":50
},
"results":[
    {
        "category":"B-Roll",
        "aspect_ratio":"16:9",
        "duration":1851,
        "hd":true,
        "title":"Title",
        "id":"video:822667",
        "type":"video",
        "keywords":"removed",
        "credit":"Removed",
        "country":"United States",
        "city":"",
        "hls_url":"file"
    },
    {
        "category":"B-Roll",
        "aspect_ratio":"16:9",
        "duration":1851,
        "hd":true,
        "title":"Title",
        "id":"video:822667",
        "type":"video",
        "keywords":"removed",
        "credit":"Removed",
        "country":"United States",
        "city":"",
        "hls_url":"file"
    },
    {
         "category":"B-Roll",
        "aspect_ratio":"16:9",
        "duration":1851,
        "hd":true,
        "title":"Title",
        "id":"video:822667",
        "type":"video",
        "keywords":"removed",
        "credit":"Removed",
        "country":"United States",
        "city":"",
        "hls_url":"file"
    }
]}

这是我的 jq 过滤器 - .results[] | {"providerName":"CrozTest" } + {"language": "en-us"} + {"lastUpdated": .timestamp} + {"shortFormVideos": [{"title": .title, "thumbnail": .thumbnail, "longDescription": .short_description, "shortDescription": .short_description, "id": .id, "releaseDate": .timestamp, "genres": ["technology"], "tags": [.branch], "content": {"duration": .duration, "dateAdded": .timestamp, "videos": [{url: .hls_url, quality: "HD", videoType: "HLS", dateAdded: .publishdate,}]}}]}

当我使用它时,它深入到 .results[],它显示一切正常,但删除了对象之间的逗号并将我的“提供者名称、语言、更新和简短视频”添加到每个对象。现在,我需要在对象之间保留逗号,并且只在文件顶部显示 provider/language/date/shortform,因为我继续操作对象以使其采用 ROKU 想要的正确格式。

这是我 运行 我的代码时显示的内容...

{
  "providerName": "CrozTest",
  "language": "en-us",
  "lastUpdated": null,
  "shortFormVideos": [
    {
      "title": "Title",
      "thumbnail": null,
      "longDescription": null,
      "shortDescription": null,
      "id": "video:822667",
      "releaseDate": null,
      "genres": [
        "technology"
      ],
      "tags": [
        null
      ],
      "content": {
        "duration": 1851,
        "dateAdded": null,
        "videos": [
          {
            "url": "file",
            "quality": "HD",
            "videoType": "HLS",
            "dateAdded": null
          }
        ]
      }
    }
  ]
}
{
  "providerName": "CrozTest",
  "language": "en-us",
  "lastUpdated": null,
  "shortFormVideos": [
    {
      "title": "Title",
      "thumbnail": null,
      "longDescription": null,
      "shortDescription": null,
      "id": "video:822667",
      "releaseDate": null,
      "genres": [
        "technology"
      ],
      "tags": [
        null
      ],
      "content": {
        "duration": 1851,
        "dateAdded": null,
        "videos": [
          {
            "url": "file",
            "quality": "HD",
            "videoType": "HLS",
            "dateAdded": null
          }
        ]
      }
    }
  ]
}
{
  "providerName": "CrozTest",
  "language": "en-us",
  "lastUpdated": null,
  "shortFormVideos": [
    {
      "title": "Title",
      "thumbnail": null,
      "longDescription": null,
      "shortDescription": null,
      "id": "video:822667",
      "releaseDate": null,
      "genres": [
        "technology"
      ],
      "tags": [
        null
      ],
      "content": {
        "duration": 1851,
        "dateAdded": null,
        "videos": [
          {
            "url": "file",
            "quality": "HD",
            "videoType": "HLS",
            "dateAdded": null
          }
        ]
      }
    }
  ]
}

现在我才刚刚开始接触 jq,这就是我想要得到的...

      {
          "providerName": "CrozTest",
          "language": "en-us",
          "lastUpdated": "2021-11-21T19:24:03.750Z",
          "shortFormVideos": [
        {
                "category":"B-Roll",
                "aspect_ratio":"16:9",
                "duration":1851,
                "hd":true,
                "title":"Title",
                "id":"video:822667",
                "type":"video",
                "keywords":"removed",
                "credit":"Removed",
                "country":"United States",
                "city":"",
                "hls_url":"file",
          "id": "video:822412",
          "releaseDate": "2021-11-21T18:21:04.353Z",
          "genres": [
            "technology"
          ],
          "tags": [
            "tag"
          ],
          "content": {
            "duration": 160,
            "dateAdded": "2021-11-21T18:21:04.353Z",
            "videos": [
              {
                "url": "hls_url",
                "quality": "HD",
                "videoType": "HLS",
                "dateAdded": "2021-11-21T18:19:31Z"
              }
            ]
          }
        },
        {
                "category":"B-Roll",
                "aspect_ratio":"16:9",
                "duration":1851,
                "hd":true,
                "title":"Title",
                "id":"video:822667",
                "type":"video",
                "keywords":"removed",
                "credit":"Removed",
                "country":"United States",
                "city":"",
                "hls_url":"file",
          "id": "video:822412",
          "releaseDate": "2021-11-21T18:21:04.353Z",
          "genres": [
            "technology"
          ],
          "tags": [
            "tag"
          ],
          "content": {
            "duration": 160,
            "dateAdded": "2021-11-21T18:21:04.353Z",
            "videos": [
              {
                "url": "hls_url",
                "quality": "HD",
                "videoType": "HLS",
                "dateAdded": "2021-11-21T18:19:31Z"
              }
            ]
          }
        },
{
                "category":"B-Roll",
                "aspect_ratio":"16:9",
                "duration":1851,
                "hd":true,
                "title":"Title",
                "id":"video:822667",
                "type":"video",
                "keywords":"removed",
                "credit":"Removed",
                "country":"United States",
                "city":"",
                "hls_url":"file",
          "id": "video:822412",
          "releaseDate": "2021-11-21T18:21:04.353Z",
          "genres": [
            "technology"
          ],
          "tags": [
            "tag"
          ],
          "content": {
            "duration": 160,
            "dateAdded": "2021-11-21T18:21:04.353Z",
            "videos": [
              {
                "url": "hls_url",
                "quality": "HD",
                "videoType": "HLS",
                "dateAdded": "2021-11-21T18:19:31Z"
              }
            ]
          }
        } 
          ]
        }

很难对此进行测试,因为您的输入几乎缺少所有字段,但是:

# header
{providerName: "CrozTest", language: "en-us", lastUpdated: (.results[].timestamp | max)} +
# the bit after the | is repeated for every element of results
{shortFormVideos: [.results[] |
  {title, thumbnail, id,
   longDescription: .short_description,
   shortDescription: .short_description,
   releaseDate: .timestamp,
   genres: ["technology"], 
   tags: [.branch],
   content:
    {duration,
     dateAdded: .timestamp, 
     videos: [{url: .hls_url,
               quality: "HD",
               videoType: "HLS",
               dateAdded: .publishdate}]}
}]}

澄清您的方法有什么问题:.results[] | ...results 的每个元素执行一次过滤器。每个过滤器的结果作为单独的 JSON 对象输出——或者如你所说——“去除逗号”。

我的方法是将 .results[] 嵌入到生成的对象中。如果你觉得它更具可读性,你也可以做 .results | map({...}) | {providerName: ......., shortFormVideos: .}