JQ: объединение глубоких данных - PullRequest
0 голосов
/ 07 марта 2019

Как мне объединить данные в stubs[], сопоставленные с port свойством.

{
  "imposters": [
    {
      "protocol": "http",
      "port": 9000,
      "name": "sanc",
      "stubs": []
    },
    {
      "protocol": "http",
      "port": 9001,
      "name": "sanp",
      "stubs": [
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/user/access/resources"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA4yRQYvCMBCF="
              }
            }
          ]
        },
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/user/access/pilot"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA6quBQAAAP//AwBDv6ajAgAAAA=="
              }
            }
          ]
        }
      ]
    }
  ]
}






{
  "imposters": [
    {
      "protocol": "http",
      "port": 9000,
      "name": "sanc",
      "stubs": []
    },
    {
      "protocol": "http",
      "port": 9001,
      "name": "sanp",
      "stubs": [
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/pay/resources"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA4yRQYvCMBCF="
              }
            }
          ]
        },
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/pay/5"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA6quBQAAAP//AwBDv6ajAgAAAA=="
              }
            }
          ]
        }
      ]
    }
  ]
}

1 Ответ

0 голосов
/ 08 марта 2019

Используя следующую команду:

jq -s '
def merge:
 {"protocol": .[0].protocol,
  "port": .[0].port,
  "name": .[0].name,
  "stubs": (reduce .[].stubs as $item ([]; . + $item) | unique)};
[[.[].imposters[]] | group_by(.port) | .[] | merge] |
{"imposters": .}' a.json

Этот тип вывода может быть сгенерирован:

{
  "imposters": [
    {
      "protocol": "http",
      "port": 9000,
      "name": "sanc",
      "stubs": []
    },
    {
      "protocol": "http",
      "port": 9001,
      "name": "sanp",
      "stubs": [
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/pay/5"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA6quBQAAAP//AwBDv6ajAgAAAA=="
              }
            }
          ]
        },
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/pay/resources"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA4yRQYvCMBCF="
              }
            }
          ]
        },
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/user/access/pilot"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA6quBQAAAP//AwBDv6ajAgAAAA=="
              }
            }
          ]
        },
        {
          "predicates": [
            {
              "deepEquals": {
                "path": "/user/access/resources"
              }
            }
          ],
          "responses": [
            {
              "is": {
                "statusCode": 200,
                "body": "H4sIAAAAAAAAA4yRQYvCMBCF="
              }
            }
          ]
        }
      ]
    }
  ]
}
...