Transformer - スコープ指定、グローバル

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:9分
  • Transformer API は、一連の定義済みルールを使用して、構造化された JSON または XML ソースコンテンツを解析し、構造化されたキーと値のペアの出力に変換するメソッドを提供します。

    この API は、 TransformerDefinition API および TransformerRuleList API と連携します。これらの API を組み合わせて、構造化された JSON ドキュメント内の XML ノードまたは任意のエンティティを、名前と値のペアの出力に変換します。文字列、数値、その他のアレイなど、アレイ内のオブジェクトと要素を含む、サポートされている JSON エンティティ。

    • TransformerRuleList API を使用すると、ソースドキュメントのどのデータを出力に含めるか、およびソースデータをどのように変換するかを定義する変換ルールリストを作成できます。

    • TransformerDefinition API は、変換ルールリストを JSON/XML レコードパスに関連付けて、再利用可能な変換定義オブジェクトを定義します。変換定義オブジェクトを使用して、1 つ以上のソースドキュメントを変換できます。

    • Transformer API は、指定された変換ルールリストを使用して、一度に 1 つのデータエンティティで実際のデータ変換を実行し、目的の出力データを作成します。

    Transformer クラスは、スコープ指定、およびグローバルのサーバースクリプトで使用できます。このクラスを使用する際は、sn_tfrm 名前空間識別子を使用します。この API をインスタンスで使用するには、Transformation Service プラグイン (com.glide.transform) をアクティブ化する必要があります。

    次の例は、TransformerRuleList API を使用して変換ルールを定義し、 TransformerDefinition API を使用して変換基準を定義して、Transformer API を使用して実際に変換を実行する方法を示しています。

    このコード例では、外部の JSON ベースの在庫に関する詳細なドキュメントを取得し、そのデータを表形式のテーブルに変換するためのルールを作成してから、一度に 1 行ずつソースドキュメントを変換します。変換される JSON ソースドキュメントのスニペットは次のとおりです。

    {
      "NOW": {
        "quote": {
          "symbol": "NOW",
          "companyName": "ServiceNow Inc.",
          "primaryExchange": "New York Stock Exchange",
          "sector": "Technology",
          "open": 166.78,
          "openTime": 1522935000556,
          "close": 165.77,
          "changePercent": 0.00656,
           ...
         },
     ...}
    var stockAPI = new sn_ws.RESTMessageV2('Stock Details', 'Default GET'); 
    var response = stockAPI.execute(); 
    var responseBody = response.getBody(); // obtain the source JSON document
    
    /* Define the list of rules to use to transform the acquired JSON stock detail 
    information into a tabular table */
    
    var transformerRuleList = new sn_tfrm.TransformerRuleList() // instantiate the rule list object
      .fromJSON() // indicate that the source document is JSON
      .addRule('ticker', '$.quote.symbol') // add a rule to copy the value in the "symbol" field of the source document to the ticker field in the output document (no changes)
      .addRule('change_percentage', '$.quote.changePercent') // copy the "changePercent" field from source into the change_percentage field of output document
      .thenMultiply('100') // multiply the change_percentage value by 100
      .thenRoundDown('0') // addthen round it down to a whole number 
      .addRule('close_price', '$.quote.close') // copy the "close" field to the close_price field in the 21=-[';output
      .thenAdaptCurrency('USD', false) // attach the US dollar code to the close_price field, but do not display the symbol
      .addRule('summary') // add a blank "summary" field to the output (no corresponding source field)
      .thenConcat('Shares of ') // in the summary field concatenate the string "Shares of "
      .thenConcatSymbol('ticker') // then concatenate the "ticker" field from the source document
      .thenConcat(' closed at ') // then concatenate the string " closed at "
      .thenConcatSymbol('close_price'); // then concatenate the "close_price" field from the source document
    
    // Create a transformer definition that associates the rule list to use and the record path of the set of records in the source document to transform.  
    var path = '$.*'; 
    var transformerDefinition = new sn_tfrm.TransformerDefinition(transformerRuleList, path);
     
    // Instantiate the transformer object.
    var transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody); 
    
    // Transform the source data, one row at a time, until all rows are processed.
    var results = []; 
    while (transformer.transform()) { 
      results.push(transformer.getRow());
    } 

    出力:

     {ticker: "Now", change_percentage: "0", close_price: "165.77 USD", summary: "Shares of Now closed at 165.77" }

    Transformer - Transformer(オブジェクト transformerDefinition, 文字列 document)

    Transformer オブジェクト (コンストラクター) をインスタンス化します。

    表 : 1. パラメーター
    名前 タイプ 説明
    transformerDefinition オブジェクト コンテンツの変換を説明するオブジェクト。ルールリストと JSONPath/XPath レコードパスが含まれます。

    このオブジェクトを生成するには、TransformerRuleList() および TransformerDefinition() API を使用します。

    document 文字列 翻訳対象のソースドキュメント。
    var stockAPI = new sn_ws.RESTMessageV2('Stock Details', 'Default GET'); 
    var response = stockAPI.execute(); 
    var responseBody = response.getBody(); 
    
    var transformerRuleList = new sn_tfrm.TransformerRuleList()
      .fromJSON()
      .addRule('ticker', 'quote.symbol') 
      .addRule('change_percentage', 'quote.change') 
      .thenMultiply('100') 
      .thenRoundDown('0') 
      .addRule('close_price', 'quote.close') 
      .thenAdaptCurrency('USD', false) 
      .addRule('summary') 
      .thenConcat('Shares of ') 
      .thenConcatSymbol('ticker') 
      .thenConcat(' closed at ') 
      .thenConcatSymbol('close') 
    
     
    var path = '$.*'; 
    var transformerDefinition = new sn_tfrm.TransformerDefinition(transformerRuleList, path); 
    var transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody); 
    
    var results = []; 
    while (transformer.transform()) { 
      results.push(transformer.getRow());
    }

    Transformer - getRow()

    前回の変換の結果の行を返します (行が存在しない場合は null)。

    表 : 2. パラメーター
    名前 タイプ 説明
    なし
    表 : 3. 返される内容
    タイプ 説明
    オブジェクト 変換されたデータの行またはノードを 1 つ含むオブジェクト。
    var results = []; 
    while (transformer.transform()) { 
      results.push(transformer.getRow());
    

    出力:

     {ticker: "Now", change_percentage: "0", close_price: "165.77 USD", summary: "Shares of Now closed at 165.77" }

    Transformer - transform()

    ソースドキュメント内の次に利用可能な行またはノードを変換します。

    表 : 4. パラメーター
    名前 タイプ 説明
    なし
    表 : 5. 返される内容
    タイプ 説明
    ブーリアン 変換対象となる有効な次の行があるかどうかを示すフラグ。
    • true:有効な次の行がある
    • false:それ以上の行がない
    var results = []; 
    while (transformer.transform()) { 
      results.push(transformer.getRow());
    

    出力:

     {ticker: "Now", change_percentage: "0", close_price: "165.77 USD", summary: "Shares of Now closed at 165.77" }