Linux

如何將單個 json 文件分離為多個 json 文件

  • December 24, 2019

我們有以下json( example 1)

範例 1

more file.json

{
 "version": 1,
 "partitions": [
   {
     "topic": "list_of_cars",
     "partition": 2,
     "replicas": [
       1003,
       1004,
       1005
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   },
   {
     "topic": "list_of_cars",
     "partition": 4,
     "replicas": [
       1005,
       1006,
       1001
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   },
   {
     "topic": "list_of_cars",
     "partition": 0,
     "replicas": [
       1001,
       1002,
       1003
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   },
   {
     "topic": "list_of_cars",
     "partition": 1,
     "replicas": [
       1002,
       1003,
       1004
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   },
   {
     "topic": "list_of_cars",
     "partition": 5,
     "replicas": [
       1006,
       1001,
       1002
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   },
   {
     "topic": "list_of_cars",
     "partition": 3,
     "replicas": [
       1004,
       1005,
       1006
     ],
     "log_dirs": [
       "any",
       "any",
       "any"
     ]
   }
 ]
}

我們想從topic(範例1)中剪切每個數組

並將其重定向到json文件中(file1.json file2.json file3.json .. 如下)

第一個文件

more file1.json

{
   "version": 1,
    "partitions": [{
       "topic": "list_of_cars",
       "partition": 2,
       "replicas": [
              1003,
              1004,
              1005
               ],
               "log_dirs": [
                "any",
                "any",
                "any"
      ]
  }]
}

第二個文件

more file2.json

{
   "version": 1,
    "partitions": [{
       "topic": "list_of_cars",
       "partition": 4,
       "replicas": [
              1005,
              1006,
              1001
               ],
               "log_dirs": [
                "any",
                "any",
                "any"
      ]
  }]
}

第三個文件

more file3.json
.
.
.

由於.partition[].partition似乎保存了任何特定分區的標識符,我們可以遍歷這些,並為每個標識符刪除沒有該特定標識符的分區。

以下假設標識符是一個沒有空格等的簡單整數:

for part in $(jq '.partitions[].partition' file.json); do
   jq --argjson part "$part" 'del(.partitions[] | select( .partition != $part ))' file.json >file-partition-"$part".json
done

這將為每個分區創建一個文件,以分區命名,而不是.partition[]數組中的索引。

引用自:https://unix.stackexchange.com/questions/558769