for文を利用したJSON形式の作成について
お世話になっております。
他のジョブAからJSON形式を受け取り、そのJSONに他のジョブBで受け取った辞書型のデータを追加するジョブを作っているのですが、for文で回していると、最後に実行されたデータでその他項目が上書きされてしまいます。
色々と試してみたのですが、解決に至らず、どこを修正すべきか教えていただけないでしょうか。
他のジョブAから受け取るJSONのイメージ
{
"test1": {
"column1": "column1 for test1",
"column2": "column2 for test1"
},
"test2": {
"column1": "column1 for test2",
"column2": "column2 for test2"
}
}
他のジョブBから受け取る辞書型データのイメージ
{ "column3": "column3" }
完成するJSONのイメージ
{
"test1": {
"column1": "column1 for test1",
"column2": "column2 for test1",
"column3": "column3 for test1"
},
"test2": {
"column1": "column1 for test2",
"column2": "column2 for test2",
"column3": "column3 for test2"
}
}
実際に記述したコード
# 変数定義
[ testJobAResult = {} ] ->
[ testJobBResult = {} ] ->
[ ResultDictionary = {} ] ->
[ detailDictionary = {
'column1' : '',
'column2' : '',
'column3' : '',
}] ->
# ジョブAの実行
[ testJobA ] ->
# JSONの受け取り
{ try |
[ testJobAResult = json_parse($RESULT) ]
} =>
# 取得したResultをforで回し、辞書に追加する
{ for elem in testJobAResult |
# 処理中のarn用のdictionaryを挿入
[ detailDictionary >> ResultDictionary[elem] ] ->
# 処理中のarnのdictionaryに情報を挿入
[ testJobAResult[elem]['column1'] >> ResultDictionary[elem]['column1']] ->
[ testJobAResult[elem]['column2'] >> ResultDictionary[elem]['column2']]
} ->
# ジョブBをジョブAのJSONの1階層目データをもとに実行
{ for elem in ResultDictionary |
[ testJobB : parameter = elem ] ->
# 辞書型の受け取り
{ try |
[ testJobBResult = $RESULT ]
} =>
# 取得した情報をJSONに付け加える
[ testJobBResult['column3'] >> ResultDictionary[elem]['column3'] ]
}
実際にできてしまうJSON
{
"test1": {
"column1": "column1 for test2",
"column2": "column2 for test2",
"column3": "column3 for test2"
},
"test2": {
"column1": "column1 for test2",
"column2": "column2 for test2",
"column3": "column3 for test2"
}
}
以上、ご確認、ご回答の程よろしくお願いいたします。
-
ご担当者様
すみません。JSONを再構築しないコーディングについても色々と試した結果、解決しました。
最終的に作成したコードは以下の通りです。
# 変数定義
[ testJobAResult = {} ] ->
[ testJobBResult = {} ] ->
[ ResultDictionary = {} ] ->
# ジョブAの実行
[ testJobA ] ->
# JSONの受け取り
{ try |
[ testJobAResult = json_parse($RESULT) ]
} =>
# 取得したResultをforで回し、辞書に追加する
{ for elem in testJobAResult |
# 処理中のarn用のdictionaryを挿入
[ testJobAResult[elem] >> ResultDictionary[elem] ]
} ->
# ジョブBをジョブAのJSONの1階層目データをもとに実行
{ for elem in ResultDictionary |
[ testJobB : parameter = elem ] ->
# 辞書型の受け取り
{ try |
[ testJobBResult = $RESULT ]
} =>
# 取得した情報をJSONに付け加える
[ testJobBResult['column3'] >> ResultDictionary[elem]['column3'] ]
}
サインインしてコメントを残してください。
コメント
3件のコメント