json - 通过jq以编程方式将URI附加到主机名?

时间:2018-02-06 16:51:08

标签: arrays json jq

我一直在使用jq来排序多个json文件,我在jq上取得了很好的成功,但在最终确定我的最终结果时被阻止了。长话短说。

我有以下json: main.json

success = static_cast<bool>(move_group.move());

。 。 。

我想将virtual_endpoint属性的URI部分附加到每个vs_endpoints(我通过jq从另一个json文件追加) 我的最终结果如下:

{
  "endpoints": [
    {
      "org": "Alison Sandbox",
      "physical_endpoint": "https://dev.service-now.com/ESD_Assets.do?SOAP",
      "physical_service": "ServiceNowService_v1_0",
      "virtual_endpoint": "https://sandbox-dv.soa.com:443/ServiceNow/ESD_ServiceNowSvc_v1_0_vs0",
      "virtual_service": "ServiceNowService_v1_0_vs0",
      "vs_endpoints": [
        "https://sandbox-ci.soa.com",
        "https://sandbox-dv101.soa.com",
        "https://sandbox-dv102.soa.com",
        "https://sandbox-dv103.soa.com",
        "https://sandbox-dv104.soa.com",
        "https://sandbox-dv105.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv107.soa.com",
        "https://sandbox-dv108.soa.com",
        "https://sandbox-dv109.soa.com",
        "https://sandbox-dv110.soa.com",
        "https://sandbox-dv111.soa.com"
      ]
    },
    {
      "org": "Architecture Frameworks",
      "physical_endpoint": "http://localhostdayofweek/DayOfWeek",
      "physical_service": "DayOfWeekServiceMT1001",
      "virtual_endpoint": "https://sandbox-dv.soa.com:443/testpromotion/DayOfWeekServiceMT1001_vs0",
      "virtual_service": "DayOfWeekServiceMT1001_vs0",
      "vs_endpoints": [
        "https://sandbox-ci.soa.com",
        "https://sandbox-dv101.soa.com",
        "https://sandbox-dv102.soa.com",
        "https://sandbox-dv103.soa.com",
        "https://sandbox-dv104.soa.com",
        "https://sandbox-dv105.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv107.soa.com",
        "https://sandbox-dv108.soa.com",
        "https://sandbox-dv109.soa.com",
        "https://sandbox-dv110.soa.com",
        "https://sandbox-dv111.soa.com"
      ]
    },
    {
      "org": "Architecture Frameworks",
      "physical_endpoint": "http://localhostdayofweek/DayOfWeek/updated",
      "physical_service": "DayOfWeekServiceMT1002",
      "virtual_endpoint": "https://sandbox-dv.soa.com:443/UPDATE/DayOfWeekServiceMT1002_vs0_2",
      "virtual_service": "DayOfWeekServiceMT1002_vs0",
      "vs_endpoints": [
        "https://sandbox-ci.soa.com",
        "https://sandbox-dv101.soa.com",
        "https://sandbox-dv102.soa.com",
        "https://sandbox-dv103.soa.com",
        "https://sandbox-dv104.soa.com",
        "https://sandbox-dv105.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv106.soa.com",
        "https://sandbox-dv107.soa.com",
        "https://sandbox-dv108.soa.com",
        "https://sandbox-dv109.soa.com",
        "https://sandbox-dv110.soa.com",
        "https://sandbox-dv111.soa.com"
      ]
    },

。 。 。 。

1 个答案:

答案 0 :(得分:1)

jq 解决方案:

jq '.endpoints |= map((.virtual_endpoint | sub("https?://[^/]+"; "")) as $end_p 
                      | .vs_endpoints |= [.[] + $end_p] )' main.json

演示: https://jqplay.org/s/ypiYcNBa-I