JSON形式の文字列を逆シリアル化して各要素の値を取得したい

Question/Problem

JSON形式の文字列をJSONオブジェクトとして扱うために逆シリアル化して各要素の値を取得する手順を教えてください

Resolution

『JSON を逆シリアル化』アクティビティを使い、JSON文字列を「JSON string」パラメータに渡しJSONオブジェクト(またはJSON配列)に変換します。

『JSON を逆シリアル化』アクティビティではJSON配列("[“で始まるJSON形式)か、あるいはJSONオブジェクト(”{"ではじまるJSON形式)かでパラメータの指定が異なります。

以下のようなJSON配列を入力値として与える場合は「入力の種類 (Input Type)」としてJArrayを指定します。

[
    {
        ""Subject"": ""Record 1 Subject"",
        ""Description"": ""This is the description for record 1.""
    },
    {
        ""Subject"": ""Record 2 Subject"",
        ""Description"": ""This is the description for record 2.""
    }
]

指定例
The image shows a configuration screen for deserializing JSON in an application, where a JSON string is converted to a JSON object with specified input and output fields. (Captioned by AI)

各要素の値を取得するには『繰り返し (コレクションの各要素)』アクティビティに『JSON を逆シリアル化』アクティビティの出力変数を渡し、次のようにします。

currentItem(“Subject”)

一方で、次のようにJSONオブジェクト形式の場合は「入力の種類 (Input Type)」としてJObjectを指定します。

{
    ""Records"": [
        {
            ""Subject"": ""Record 1 Subject"",
            ""Description"": ""This is the description for record 1.""
        },
        {
            ""Subject"": ""Record 2 Subject"",
            ""Description"": ""This is the description for record 2.""
        }
    ]
}

指定例
This image shows a "Deserialize JSON" interface with fields for a JSON string, optional JSON sample, input type, and output JSON object. (Captioned by AI)

JSONオブジェクト(JObject)を指定した場合、出力変数を次の『繰り返し (コレクションの各要素)』アクティビティの入力として与えます。
The image shows a "For Each currentItem" loop configuration interface, specifying a JSON object to iterate over with the item name set as "currentItem." (Captioned by AI)

更にループの内側に『繰り返し (コレクションの各要素)』アクティビティを入れ子にして使い 外側のループの内部変数 currentItemのValueの値を入力として与えます。

image2024-8-4_9-4-53

すると、Subject や Description といった各要素の値は次のように取得することができます。

currentJToken(“Subject”)

currentJToken(“Description”)

※文字列変数に代入するときはcurrentJToken( "Subject" ).toStringとします。