LoginSignup
25
20

More than 5 years have passed since last update.

jq: jsonのkey/valueの追加、arrayへ要素追加

Last updated at Posted at 2017-01-22

やりたいこと

シェルとjqを使って、下記の "org.json" に キー名 "inputFiles" を付け加えて、なんとか "result3.json" のように変更したい。

これを

org.json
{
  "name": "BringYourOwnLAMMPS",
  "jobanalyses": [
    {
      "useMpi": true,
      "command": "./airFoil2D/Allrun",
      "analysis": {
        "code": "user_included_mpi",
        "name": "Bring Your Own MPI Software",
        "version": "openmpi-1.6-el6"
      },
      "hardware": {
        "coresPerSlot": 1,
        "slots": 1,
        "coreType": "hpc-plus"
      }
    }
  ]
}

このようにしたい!!!

result3.json
{
  "name": "BringYourOwnLAMMPS",
  "jobanalyses": [
    {
      "useMpi": true,
      "command": "./airFoil2D/Allrun",
      "analysis": {
        "code": "user_included_mpi",
        "name": "Bring Your Own MPI Software",
        "version": "openmpi-1.6-el6"
      },
      "hardware": {
        "coresPerSlot": 1,
        "slots": 1,
        "coreType": "hpc-plus"
      },
      "inputFiles": [
        {
          "id": "myId1"
        },
        {
          "id": "myId2"
        }
      ]
    }
  ]
}

動作確認

今回はわかりやすさのため、段階を追って、org.json から result3.json までたどり着きたいと思います。

test用 JSONファイル "org.json" の作成

テストようにファイル org.json を作ります

org.json
cat << EOF > org.json
{
  "name": "BringYourOwnLAMMPS",
  "jobanalyses": [
    {
      "useMpi": true,
      "command": "./airFoil2D/Allrun",
      "analysis": {
        "code": "user_included_mpi",
        "name": "Bring Your Own MPI Software",
        "version": "openmpi-1.6-el6"
      },
      "hardware": {
        "coresPerSlot": 1,
        "slots": 1,
        "coreType": "hpc-plus"
      }
    }
  ]
}
EOF
cat org.json

Key-Valueの追加

"jobanalyses" の中に "inputFiles" という "key-value" を追加します

key-valueの追加
# 結果を保存するファイル名を決めます
RESULT_JSON='result1.json'

# jqを実行します
cat org.json | jq '.jobanalyses[0] |= .+ {"inputFiles": []}' \
> ${RESULT_JSON} && cat ${RESULT_JSON}
結果(result1.json)
{
  "name": "BringYourOwnLAMMPS",
  "jobanalyses": [
    {
      "useMpi": true,
      "command": "./airFoil2D/Allrun",
      "analysis": {
        "code": "user_included_mpi",
        "name": "Bring Your Own MPI Software",
        "version": "openmpi-1.6-el6"
      },
      "hardware": {
        "coresPerSlot": 1,
        "slots": 1,
        "coreType": "hpc-plus"
      },
      "inputFiles": []
    }
  ]
}

配列の要素を追加 (1)

result1.json inputFilesの配列に対してKey="id"とVlue="myId1"を追加します

配列の追加
# 結果を保存するファイル名を決めます
RESULT_JSON='result2.json'

# jqを実行します
cat result1.json | jq '.jobanalyses[0].inputFiles[0] |= .+{"id": "myId1"}' \
> ${RESULT_JSON} && cat ${RESULT_JSON}

もしくは、下記。配列の指定がない分こちらの方が楽かも・・・

配列の追加
# 結果を保存するファイル名を決めます
RESULT_JSON='result2.json'

# jqを実行します
cat result1.json | jq '.jobanalyses[0].inputFiles |= .+[{"id": "myId1"}]' \
> ${RESULT_JSON} && cat ${RESULT_JSON}
結果(result2.json)
{
  "name": "BringYourOwnLAMMPS",
  "jobanalyses": [
    {
      "useMpi": true,
      "command": "./airFoil2D/Allrun",
      "analysis": {
        "code": "user_included_mpi",
        "name": "Bring Your Own MPI Software",
        "version": "openmpi-1.6-el6"
      },
      "hardware": {
        "coresPerSlot": 1,
        "slots": 1,
        "coreType": "hpc-plus"
      },
      "inputFiles": [
        {
          "id": "myId1"
        }
      ]
    }
  ]
}

配列の要素を追加 (2)

さらにresult2.json inputFilesの配列に対してKey="id"とVlue="myId2"を追加します

配列の追加
# 結果を保存するファイル名を決めます
RESULT_JSON='result3.json'

# jqを実行します
cat result2.json | jq '.jobanalyses[0].inputFiles[1] |= .+{"id": "myId2"}' \
> ${RESULT_JSON} && cat ${RESULT_JSON}

もしくは、下記。配列の指定がない分こちらの方が楽かも・・・

配列の追加
# 結果を保存するファイル名を決めます
RESULT_JSON='result3.json'

# jqを実行します
cat result2.json | jq '.jobanalyses[0].inputFiles |= .+[{"id": "myId2"}]' \
> ${RESULT_JSON} && cat ${RESULT_JSON}
結果(result3.json)
{
  "useMpi": true,
  "command": "./airFoil2D/Allrun",
  "analysis": {
    "code": "user_included_mpi",
    "name": "Bring Your Own MPI Software",
    "version": "openmpi-1.6-el6"
  },
  "hardware": {
    "coresPerSlot": 1,
    "slots": 1,
    "coreType": "hpc-plus"
  },
  "inputFiles": [
    {
      "id": "myId1"
    },
    {
      "id": "myId2"
    }
  ]
}

参考

penguin_dreamさんありがとうございます。
おかげでやりたいことができました。

25
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
20