3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IBM Watson ConversationでコンテキストのJSONを操作したいとき

Posted at

会話の最中に以下のコンテキストを保持していたとして

{
  "context": {
    ...
    "complex_object": {
      "user_firstname" : "Paul",
      "user_lastname" : "Pan"
      "has_card" : false
    }
  }
}

ノードからこのようなコンテキストのアップデートをかけられたとします

{
  "complex_object": {
    "user_firstname": "Peter",
    "has_card": true
  }
}

すると結果は

{
  "complex_object": {
    "user_firstname": "Peter",
    "user_lastname": "Pan",
    "has_card": true
  }
}

過去に持っていた値を保持しつつ、アップデートで該当する値は上書きされます

配列への値の追加

このtoppings_arrayという配列に値を追加したい場合

{
  "context": {
    "toppings_array": ["onion", "olives"]
  }
}

このようなメソッドが使えます

{
  "context": {
    "toppings_array": "<? $toppings_array.append('ketchup', 'tomatoes') ?>"
  }
}

contextに含まれるtoppings_arrayを$toppings_arrayというリストオブジェクトとして扱っています
そこからappendメソッドを呼び出しています

その結果

{
  "context": {
    "toppings_array": ["onion", "olives", "ketchup", "tomatoes"]
  }
}

このようになります

配列から値を削除

remove(key)かremoveValue(value)で行えます

{
  "context": {
    "toppings_array": "<? $toppings_array.remove(0) ?>"
  }
}
{
  "context": {
    "toppings_array": "<? $toppings_array.removeValue('onion') ?>"
  }
}

結果はどちらも

{
  "context": {
    "toppings_array": ["olives", "ketchup", "tomatoes"]
  }
}

このようになります

3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?