Итерация по JSON с использованием jq и преобразование в CSV - PullRequest
1 голос
/ 22 апреля 2020

У меня есть следующая JSON полезная нагрузка, которую я хотел бы сгладить с помощью jq, а затем преобразовать в CSV.

Проблема, с которой я столкнулся, заключается в том, что я не уверен, что делать с books, movies и dvds массивы, так как каждый из них имеет различное количество результатов.

{
  "report": {

    "clients": [
      {
        "id": "001",
        "name": "client_1",

        "books": [
          {"sku": "100", "description": "book title: book-A"},
          {"sku": "101", "description": "book title: book-B"},
          {"sku": "102", "description": "book title: book-C"}
        ],

        "movies": [
          {"sku": "200", "description": "movie title: movie-A"},
          {"sku": "201", "description": "movie title: movie-B"}
        ],

        "dvds": [
          {"sku": "300", "description": "dvd title: dvd-A"}
        ]
      },

      {
        "id": "002",
        "name": "client_2",

        "books": [
          {"sku": "100", "description": "book title: book-A"},
          {"sku": "101", "description": "book title: book-B"},
          {"sku": "102", "description": "book title: book-C"},
          {"sku": "103", "description": "book title: book-D"},
          {"sku": "104", "description": "book title: book-E"}
        ],

        "movies": [
          {"sku": "200", "description": "movie title: movie-A"}

        ],

        "dvds": [
          {"sku": "300", "description": "dvd title: dvd-A"},
          {"sku": "301", "description": "dvd title: dvd-B"},
          {"sku": "302", "description": "dvd title: dvd-C"},
          {"sku": "303", "description": "dvd title: dvd-D"}
        ]
      }

    ]
  }
}

Для каждого book, movie и dvd клиента есть, мне нужно сослаться на идентификатор клиента и имя. Ниже приведено представление результатов, которые я хотел бы получить:

c_id      | c_name   | type   | sku | description            |
----------|----------|--------|-----|------------------------|
001       | client_1 | books  | 100 | book title: book-A     |
001       | client_1 | books  | 101 | book title: book-B     |
001       | client_1 | books  | 102 | book title: book-C     |
001       | client_1 | movies | 200 | movie title: movie-A   |
001       | client_1 | movies | 201 | movie title: movie-B   |
001       | client_1 | dvds   | 300 | dvd title: dvd-A       |
002       | client_2 | books  | 100 | book title: book-A     |
002       | client_2 | books  | 101 | book title: book-B     |
002       | client_2 | books  | 102 | book title: book-C     |
002       | client_2 | books  | 103 | book title: book-D     |
002       | client_2 | books  | 104 | book title: book-E     |
002       | client_2 | movies | 200 | movie title: movie-A   |
002       | client_2 | dvds   | 300 | dvd title: dvd-A       |
002       | client_2 | dvds   | 301 | dvd title: dvd-B       |
002       | client_2 | dvds   | 302 | dvd title: dvd-C       |
002       | client_2 | dvds   | 303 | dvd title: dvd-D       |

вот мой фрагмент jq

Любая помощь будет принята с благодарностью.

1 Ответ

2 голосов
/ 22 апреля 2020

Здесь вы go

jq --compact-output '.report.clients[] | [.id, .name] + ((.books[] | ["books", .sku, .description]), (.movies[] | ["movie", .sku, .description]), (.dvds[] | ["dvds", .sku, .description]))'

["001","client_1","books","100","book title: book-A"]
["001","client_1","books","101","book title: book-B"]
["001","client_1","books","102","book title: book-C"]
["001","client_1","movie","200","movie title: movie-A"]
["001","client_1","movie","201","movie title: movie-B"]
["001","client_1","dvds","300","dvd title: dvd-A"]
["002","client_2","books","100","book title: book-A"]
["002","client_2","books","101","book title: book-B"]
["002","client_2","books","102","book title: book-C"]
["002","client_2","books","103","book title: book-D"]
["002","client_2","books","104","book title: book-E"]
["002","client_2","movie","200","movie title: movie-A"]
["002","client_2","dvds","300","dvd title: dvd-A"]
["002","client_2","dvds","301","dvd title: dvd-B"]
["002","client_2","dvds","302","dvd title: dvd-C"]
["002","client_2","dvds","303","dvd title: dvd-D"]

Попробуйте онлайн!

Если вы хотите истинный CSV, вы можете добавить | @csv в конце

jq --compact-output '.report.clients[] | [.id, .name] + ((.books[] | ["books", .sku, .description]), (.movies[] | ["movie", .sku, .description]), (.dvds[] | ["dvds", .sku, .description])) | @csv'

"\"001\",\"client_1\",\"books\",\"100\",\"book title: book-A\""
"\"001\",\"client_1\",\"books\",\"101\",\"book title: book-B\""
"\"001\",\"client_1\",\"books\",\"102\",\"book title: book-C\""
"\"001\",\"client_1\",\"movie\",\"200\",\"movie title: movie-A\""
"\"001\",\"client_1\",\"movie\",\"201\",\"movie title: movie-B\""
"\"001\",\"client_1\",\"dvds\",\"300\",\"dvd title: dvd-A\""
"\"002\",\"client_2\",\"books\",\"100\",\"book title: book-A\""
"\"002\",\"client_2\",\"books\",\"101\",\"book title: book-B\""
"\"002\",\"client_2\",\"books\",\"102\",\"book title: book-C\""
"\"002\",\"client_2\",\"books\",\"103\",\"book title: book-D\""
"\"002\",\"client_2\",\"books\",\"104\",\"book title: book-E\""
"\"002\",\"client_2\",\"movie\",\"200\",\"movie title: movie-A\""
"\"002\",\"client_2\",\"dvds\",\"300\",\"dvd title: dvd-A\""
"\"002\",\"client_2\",\"dvds\",\"301\",\"dvd title: dvd-B\""
"\"002\",\"client_2\",\"dvds\",\"302\",\"dvd title: dvd-C\""
"\"002\",\"client_2\",\"dvds\",\"303\",\"dvd title: dvd-D\""

Попробуйте онлайн!

Если вы хотите что-то CSV-i sh, но не настоящий CSV, взгляните на join. Например,

jq --compact-output --raw-output '.report.clients[] | [.id, .name] + ((.books[] | ["books", .sku, .description]), (.movies[] | ["movie", .sku, .description]), (.dvds[] | ["dvds", .sku, .description])) | join("|")'

001|client_1|books|100|book title: book-A
001|client_1|books|101|book title: book-B
001|client_1|books|102|book title: book-C
001|client_1|movie|200|movie title: movie-A
001|client_1|movie|201|movie title: movie-B
001|client_1|dvds|300|dvd title: dvd-A
002|client_2|books|100|book title: book-A
002|client_2|books|101|book title: book-B
002|client_2|books|102|book title: book-C
002|client_2|books|103|book title: book-D
002|client_2|books|104|book title: book-E
002|client_2|movie|200|movie title: movie-A
002|client_2|dvds|300|dvd title: dvd-A
002|client_2|dvds|301|dvd title: dvd-B
002|client_2|dvds|302|dvd title: dvd-C
002|client_2|dvds|303|dvd title: dvd-D

Попробуйте онлайн!

Дайте мне знать, если у вас есть какие-либо вопросы. Надеюсь, это поможет!

...