我正在构建一个ROKU应用程序,并且正在重新格式化一个从API中提取的json文件,使其与ROKU Direct Publisher feed的格式相同。
这就是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"
}
]
}
}
]
}发布于 2021-11-23 12:27:17
这很难测试,因为您的输入几乎缺少它的所有字段,但是:
# 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: .}
https://stackoverflow.com/questions/70074192
复制相似问题