Skip to content

Instantly share code, notes, and snippets.

@itochan
Created March 10, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itochan/caf23c5d334b4508283e to your computer and use it in GitHub Desktop.
Save itochan/caf23c5d334b4508283e to your computer and use it in GitHub Desktop.
jq -s '{ response: [( (.[0].response | sort_by(.name)) as $a | (.[1].response | sort_by(.name)) as $b | ($a | length) as $length | $a[] as $item | $item + $b[range(0;$length)] )] }' a.json b.json

a.json

{
  "response": [
    {
      "station_ja": "新宿",
      "lines": [
        "yamanote",
        "chuo"
      ]
    },
    {
      "station_ja": "代々木",
      "lines": [
        "yamanote",
        "chuo"
      ]
    },
    {
      "station_ja": "千駄ヶ谷",
      "lines": [
        "chuo"
      ]
    }
  ]
}

b.json

{
  "response": [
    {
      "station_ja": "新宿",
      "station_en": "Shinjuku"
    },
    {
      "station_ja": "代々木",
      "station_en": "Yoyogi"
    },
    {
      "station_ja": "千駄ヶ谷",
      "station_en": "Sendagaya"
    }
  ]
}

output

{
  "response": [
    {
      "station_ja": "新宿",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Shinjuku"
    },
    {
      "station_ja": "代々木",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Yoyogi"
    },
    {
      "station_ja": "千駄ヶ谷",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Sendagaya"
    },
    {
      "station_ja": "新宿",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Shinjuku"
    },
    {
      "station_ja": "代々木",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Yoyogi"
    },
    {
      "station_ja": "千駄ヶ谷",
      "lines": [
        "yamanote",
        "chuo"
      ],
      "station_en": "Sendagaya"
    },
    {
      "station_ja": "新宿",
      "lines": [
        "chuo"
      ],
      "station_en": "Shinjuku"
    },
    {
      "station_ja": "代々木",
      "lines": [
        "chuo"
      ],
      "station_en": "Yoyogi"
    },
    {
      "station_ja": "千駄ヶ谷",
      "lines": [
        "chuo"
      ],
      "station_en": "Sendagaya"
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment