3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Node-REDAdvent Calendar 2024

Day 25

JSONataオブジェクト操作チートシート

Last updated at Posted at 2024-12-24

JSONataの基本

JSONataの基本的なことは以下がわかりやすいです。

本題: JSONataオブジェクト操作チートシート

Node-REDのフローで役に立つJSONataによるオブジェクト操作のチートシートです(※Beforeの値がmsg.payloadを流れてきた前提)

1. オブジェクト内の特定のキー名を変更する

Before:

{
  "name": "Alice",
  "age": 30
}

JSONata:

{
  "fullName": payload.name,
  "age": payload.age
}

After:

{
  "fullName": "Alice",
  "age": 30
}

2. ネストされたオブジェクトをフラットにする

Before:

{
  "name": "Bob",
  "details": {
    "age": 25,
    "city": "New York"
  }
}

JSONata:

{
  "name": payload.name,
  "age": payload.details.age,
  "city": payload.details.city
}

After:

{
  "name": "Bob",
  "age": 25,
  "city": "New York"
}

3. 2が配列の場合

Before:

[
  {
    "name": "Joe",
    "details": {
      "age": 18,
      "city": "Texas City"
    }  
  },
  {
    "name": "Charlie",
    "details": {
      "age": 22,
      "city": "San Francisco"
    }
  },
  {
    "name": "Diana",
    "details": {
      "age": 28,
      "city": "New York"
    }
  }
]

JSONata:

payload.{
  "name": name,
  "age": details.age,
  "city": details.city
}

After:

[
  {
    "name": "Joe",
    "age": 18,
    "city": "Texas City"
  },
  {
    "name": "Charlie",
    "age": 22,
    "city": "San Francisco"
  },
  {
    "name": "Diana",
    "age": 28,
    "city": "New York"
  }
]

4. 配列内のオブジェクトの特定のフィールドだけを抽出する

Before:

[
  { 
    "name": "Joe", 
    "age": 18 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

JSONata:

payload.name

After:

["Joe", "Charlie", "Diana"]

5. 条件によるフィルタリング

Before:

[
  { 
    "name": "Joe", 
    "age": 18 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

JSONata:

payload[age >= 21]

After:

[
  {
    "name": "Charlie",
    "age": 22
  },
  {
    "name": "Diana",
    "age": 28
  }
]

6. 配列内のオブジェクトのキーを別の値に変換する

Before:

[
  { "oldKey": "value1" },
  { "oldKey": "value2" }
]

JSONata:

payload.{ "newKey": oldKey }

After:

[
  {"newKey": "value1"},
  {"newKey": "value2"}
]

7. 配列の要素を削除する(特定の値を持つ要素を除外)

Before:

[
  {
    "title": "task1",
    "status": "completed"
  },
  {
     "title": "task2",
     "status": "pending"
  },
  {
    "title": "task3",
    "status": "progress"
  }
]

JSONata:

payload[status != "completed"]

After:

[
  {
    "title": "task2",
    "status":"pending"
  },
  {
    "title":"task3",
    "status":"progress"
  }
]

8. 数値の集計(合計)

Before:

[
  { "price": 100 },
  { "price": 200 },
  { "price": 150 }
]

JSONata:

$sum(payload.price)

After:

450

9. デフォルト値を適用する(値が存在しない場合)

Before:

[
  { 
    "name": "Joe", 
    "age": null 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

JSONata:

payload.{ "name": name, "age": age ? age : 18 }

After:

[
  { 
    "name": "Joe", 
    "age": 18 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

10. オブジェクトから特定のキーを削除する

Before:

{
  "user": {
    "id": 12345,
    "name": "Helen",
    "password": "secret"
  }
}

JSONata:

{
  "id": payload.user.id,
  "name": payload.user.name
}

After:

{
  "id": 12345,
  "name": "Helen"
}

11. 配列の長さを取得する

Before:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 },
  { "name": "Diana", "age": 28 }
]

JSONata:

$count(payload)

After:

3

12. 配列の特定の範囲を取得する

Before:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 },
  { "name": "Diana", "age": 28 },
  { "name": "Heima", "age": 38 }
]

JSONata:

payload[[1..3]]

After:

[
  {
    "name": "Charlie",
    "age": 22
  },
  {
    "name": "Diana",
    "age": 28
  },
  {
    "name": "Heima",
    "age": 38
  }
]

13. 配列の要素をソートする

Before:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 },
  { "name": "Diana", "age": 28 }
]

JSONata:

payload^(-age)

After:

[
  {
    "name": "Diana",
    "age": 28
  },
  {
    "name": "Charlie",
    "age": 22
  },
  {
    "name": "Joe",
    "age": 18
  }
]

14. 条件に基づいた値の変換

Before:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 },
  { "name": "Diana", "age": 28 }
]

JSONata:

payload.{ "name": name, "category": age >= 21 ? "adult" : "minor" }

After:

[
  { "name": "Joe", "category": "minor" },
  { "name": "Charlie", "category": "adult" },
  { "name": "Diana", "category": "adult" }
]

15. 重複する要素の削除

Before:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 },
  { "name": "Joe", "age": 18 }
]

JSONata:

$distinct(payload)

After:

[
  { "name": "Joe", "age": 18 },
  { "name": "Charlie", "age": 22 }
]

16. 部分一致によるフィルタリング

Before:

[
  { "title": "Node-RED tutorial" },
  { "title": "JSONata guide" },
  { "title": "JavaScript basics" }
]

JSONata:

payload[title ~> /.*tutorial.*/]

After:

[
  { "title": "Node-RED tutorial" }
]

17. 特定の値をキーとして新しいオブジェクトを作る

Before:

[
  { 
    "name": "Joe", 
    "age": 18 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

JSONata:

payload.{ $lookup("name"): $ }

After:

[
  { 
    "Joe": {
      "name": "Joe", 
      "age": 18 
    }
  },
  {
    "Charlie": {
      "name": "Charlie", 
      "age": 22
    }
  }, 
  {
    "Diana": {
      "name": "Diana",
      "age": 28
    }
  }
]

18. 17の逆

Before:

[
  { 
    "Joe": {
      "name": "Joe", 
      "age": 18 
    }
  },
  {
    "Charlie": {
      "name": "Charlie", 
      "age": 22
    }
  }, 
  {
    "Diana": {
      "name": "Diana",
      "age": 28
    }
  }
]

JSONata:

payload.*

After:

[
  { 
    "name": "Joe", 
    "age": 18 
  },
  { 
    "name": "Charlie", 
    "age": 22 
  },
  { 
    "name": "Diana", 
    "age": 28 
  }
]

19. 17の逆で且つネストをフラットにする

Before:

[
  {
    "Joe": {
      "name": "Joe",
      "details": {
        "age": 18,
        "city": "Texas City"
      }
    }
  },
  {
    "Charlie": {
      "name": "Charlie",
      "details": {
        "age": 22,
        "city": "San Francisco"
      }
    }
  },
  {
    "Diana": {
      "name": "Diana",
      "details": {
        "age": 28,
        "city": "New York"
      }
    }
  }
]

JSONata:

payload.{
  "name": $lookup($, $keys())[0].name,
  "age": $lookup($, $keys())[0].details.age,
  "city": $lookup($, $keys())[0].details.city
}

After:

[
  {
    "name": "Joe",
    "age": 18,
    "city": "Texas City"
  },
  {
    "name": "Charlie",
    "age": 22,
    "city": "San Francisco"
  },
  {
    "name": "Diana",
    "age": 28,
    "city": "New York"
  }
]

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?