尝试创建地图,但以JQ中的List结尾

时间:2019-03-07 16:21:24

标签: json dictionary jq

我正在尝试在给定的输出结构中获取下面给出的Input JSON,但我正在将结构作为List而不是Map。

输入结构:

{

      "errorMessage": null,
      "errorCode": null,
      "state": "Complete",
       "status": "Completed",
       "createdDate": "234234234234",
       "createdTime": "332432423433",

      "orderId": {
        "Order394": {
          "orderId": "Order394",
          "targetRequirements": [

            { "targetID" : "1234",
            "targetDate" : "targetDate",
              "preferenceOrder": 1,
              "matchID": "502"
            },
            { "targetID" : "1235",
            "targetDate" : "targetDate",
              "preferenceOrder": 1,
              "matchID": "503"
            }
          ],
          "username": "Arun",
          "address": "6166192",
          "Title": "Kumar"
        },
        "Order395": {
          "orderId": "Order395",
          "targetRequirements": [

            { "targetDate" : "targetDate",
           "targetID" : "1234",
              "preferenceOrder": 1,
              "matchID": "502"
            }
          ],
          "username": "Arun",
          "address": "6166192",
          "Title": "Kumar"
        }
      }
    }

预期的响应:

{
      "errorCode": null,
      "errorMessage": null,
      "orderId": {
        "Order394": {
          "orderId": "Order394",
          "targetRequirements": [
            {
              "matchID": "502",
              "preferenceOrder": 1
            },
            {
              "matchID": "503",
              "preferenceOrder": 1
            }
          ]
        },
        "Order395": {
          "orderId": "Order395",
          "targetRequirements": [
            {
              "matchID": "502",
              "preferenceOrder": 1
            }
          ]
        }
      }
    }

我尝试使用下面给出的JQ查询,但这正在创建orderId的LIST,但是我需要一张地图。

{ errorMessage,errorCode,  orderId :  (.orderId |                               map({    (.orderId) : {   orderId ,          targetRequirements : (.targetRequirements |              map({             preferenceOrder               ,targetID             })            )      }   }) )  }

示例工作示例可在https://jqplay.org/s/exd2QjrS9V

中找到

先谢谢了。 阿伦库马尔

1 个答案:

答案 0 :(得分:1)

似乎您缺少对add的调用。将add添加到您的jq:

{ errorMessage,
  errorCode,
  orderId:
    (.orderId
     | map({ (.orderId) :
               { orderId,
                 targetRequirements:
                   (.targetRequirements
                    | map({preferenceOrder,targetID }) ) } })
     | add ) }

这会产生所需的结构,但是输出在某些细节上与预期输出有所不同,因此您可能需要相应地调整过滤器。