Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
12
Help us understand the problem. What is going on with this article?
@daikumatan

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

More than 3 years have passed since last update.

やりたいこと

シェルと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さんありがとうございます。
おかげでやりたいことができました。

12
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
daikumatan
2002-2015: Fujixerox, Numerical simulation Engineer 2015-2016: NVIDIA Japan, BD Manager 2016-2020: Rescale Japan, Evangelist 2020-Present: XTREME-D, CTO

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
12
Help us understand the problem. What is going on with this article?