みんなに優しく、解りやすくをモットーに開設しています。 以下のルールを守りみんなで助け合いましょう。
1.ファイルメーカーで解らない事があればここで質問して下さい。 何方でも、ご質問・ご回答お願いします。 (優しく回答しましょう)
You are not logged in.
Pages: 1
URLから挿入で取得したJSON形式のデータですが、欲しいデータの抽出がなかなかうまくいきません。
環境はwindows 10 Filemaker pro 16 です。
取得データ
{"body":{"productSets":{"productSet":[{"dealerProductCode":"A_1","stock":5},
{"dealerProductCode":"A_2",,"stock":5},
{"dealerProductCode":"B_1","stock":0},
{"dealerProductCode":"B_2","stock":2},
{"dealerProductCode":"B_3","stock":0},
{"dealerProductCode":"C_1","stock":4},
{"dealerProductCode":"C_2","stock":3},
{"dealerProductCode":"D_1","stock":2},
]}}}
※実際の取得データは"dealerProductCode"が2000程あります。(データは重複していません)
例えばこのデータから"B_2"を検索して"stock"の「2」を抽出したいのですが、どうすればうまくいきますでしょうか。
よろしくお願いいたします。
Offline
16環境無いので未検証のお気楽レスですが、
取得JSONデータ
{"body":
{"productSets":
{"productSet":
[
{"dealerProductCode":"A_1","stock":5},
{"dealerProductCode":"A_2","stock":5},
{"dealerProductCode":"B_1","stock":0},
{"dealerProductCode":"B_2","stock":2},
{"dealerProductCode":"B_3","stock":0},
{"dealerProductCode":"C_1","stock":4},
{"dealerProductCode":"C_2","stock":3},
{"dealerProductCode":"D_1","stock":2}
]
}
}
}
とすると、取得関数式は以下とかかな?
JSONGetElement ( 取得データ ; "body.productSets.productSet[3]stock" )
Last edited by Hiro (2017-11-29 02:37:41)
Offline
Hiroさん、ご返信ありがとうございます。
"B_2"というデータを使用して抽出はできないでしょうか。
もし"Z_2"のstockデータが欲しいとなった場合、2000程のデータからどの位置にあるのか分からないので索引を設定するのが難しいです。
よろしくお願い致します。
Offline
FileMaker Pro 16 のJSON関連関数では、値を検索するような関数がないので、以下のような感じで、対象Value のデータが取得できます。
フィールド:json::JSON |JSON文字列
フィールド:json::SearchValue |検索値( C_2 など)
変数を設定 [ $JSON; 値:json::JSON ]
変数を設定 [ $SearchValue; 値:json::SearchValue ]
#要素をリストで取得
変数を設定 [ $JSONListValues; 値:JSONListValues ( json::JSON ; "body.productSets.productSet" ) ]
#対象の検索値までのポジションを取得
変数を設定 [ $ValuesPosition; 値:Position ( $JSONListValues ; $SearchValue ; 1 ; 1 ) ]
#ポジションまでの改行を数える
変数を設定 [ $Index; 値:PatternCount ( Left ( $JSONListValues ; $ValuesPosition ) ; ¶ ) ]
#改行数をIndexとしてstockの値を取得
変数を設定 [ $Result; 値:JSONGetElement ( $JSON ; "body.productSets.productSet[" & $Index & "]stock" ) ]
Last edited by qb_dp (2017-11-29 14:49:04)
Offline
おまけ。
PowerShellだとValueを検索して目的の要素の取得が容易に行なえます。
変数を設定 [ $JSON; 値:json::JSON ]
変数を設定 [ $SearchValue; 値:json::SearchValue ]
変数を設定 [ $PS; 値:Let([ PS="
$DATA=\"{DATA}\";
$JSON= $DATA| ConvertFrom-Json;
$Element=$JSON.body.productSets.productSet | where { $_.dealerProductCode -eq \"{SearchValue}\" };
$Element.stock; "
];
Substitute ( PS ;
["{DATA}" ; Substitute ( $JSON ; "\"" ; "`\"" )]
; ["{SearchValue}" ; Substitute ( $SearchValue ; "\"" ; "`\"" )]
)
) ]
変数を設定 [ $Result; 値:SMPS_Exe( $PS ) ]
戻り値の取得の為にプラグインを使用します。
ScriptMakerPS | FileMaker Plugin for Windows
https://sites.google.com/site/scriptmakerps/
Offline
#3 > "B_2"というデータを使用して抽出はできないでしょうか。
私もqd_dpさん#4案に同じです。先を越されました...。
Offline
qb_dpさん、ありがとうございます。
早速試してみたいと思います。
Hiroさんもありがとうございました。
Offline
#4のスクリプトを等価式に換えて、計算フィールドに直接一発で算出することもできますネ。
Let(
[
#JSON=JSONフィールド;
#SearchValue=SearchValueフィールド;
//▼要素をリストで取得
#JSONListValues=JSONListValues(#JSON; "body.productSets.productSet");
//▼対象の検索値までのポジションを取得
#ValuesPosition=Position(#JSONListValues; #SearchValue; 1; 1);
//▼ポジションまでの改行を数える
#Index=PatternCount(Left(#JSONListValues; #ValuesPosition); ¶)
];
//▼改行数をIndexとしてstockの値を取得
JSONGetElement(#JSON; "body.productSets.productSet[" & #Index & "]stock")
)
Offline
Hiroさん、ありがとうございます。
こんな方法もあるのですね。
いろいろ勉強になります。
ありがとうございました。
Offline
Pages: 1
[ Generated in 0.007 seconds, 9 queries executed - Memory usage: 521.33 KiB (Peak: 526.23 KiB) ]