Zum Inhalt springen

JSON-Funktionen im Jitterbit Studio

Einführung

JavaScript Object Notation (JSON) Funktionen ermöglichen die Manipulation von Daten im JSON-Format. Für weitere Informationen zu JSON siehe IETF RFC 8259: Das JavaScript Object Notation (JSON) Datenformat.

GetJSONString

Deklaration

string GetJSONString(string json_string, string path)

Syntax

GetJSONString(<json_string>, <path>)

Erforderliche Parameter

  • json_string: Ein JSON-Objekt-String, aus dem Daten geparst werden sollen.
  • path: Ein gültiger Pfad, der den Standort der Daten im JSON-Objekt-String darstellt.

Beschreibung

Ruft Daten aus einem JSON-Objekt-String unter Verwendung des angegebenen Pfades ab. Wenn der Pfad ungültig ist, wird der Ausführungsfluss unterbrochen und eine Fehlermeldung zurückgegeben.

Wichtig

Eine verbesserte Version dieser Funktion, GetJSONStringEx, bietet bessere Fehlerbehandlungsfunktionen, einschließlich Kontrolle über die zurückgegebene Nachricht, wenn es ein Problem mit dem angegebenen Pfad gibt.

Diese Funktion erfordert die Agenten-Version 11.28 oder höher.

Beispiele

// Define the JSON object string:
json_string = '{ "company": [{ "name": "Jitterbit", "product": [{ "type": "iPaaS", "name": "Jitterbit iPaaS" },{ "type": "EDI", "name": "Jitterbit EDI" }] }] }';

GetJSONString(json_string, "/company/[0]/product/[1]/name");
// Returns "Jitterbit EDI"

GetJSONString(json_string, "/company/[0]/product");
// Returns '[{"type":"iPaaS","name":"Jitterbit iPaaS"},{"type":"EDI","name":"Jitterbit EDI"}]'

GetJSONString(json_string, "/company/[0]/employees");
// Returns an "employees is not a valid path" error because companies do not have employees defined in the JSON object string

GetJSONString(json_string, "/company/[1]");
// Returns a "1 is out of range" error because only one company is defined in the JSON object string

GetJSONStringEx

Deklaration

string GetJSONStringEx(string json_string, string path[, string value_if_no_match])

Syntax

GetJSONStringEx(<json_string>, <path>[, <value_if_no_match>])

Erforderliche Parameter

  • json_string: Ein JSON-Objekt-String, aus dem Daten geparst werden sollen.
  • path: Ein gültiger Pfad, der den Standort der Daten im JSON-Objekt-String darstellt.

Optionale Parameter

  • value_if_no_match: Ein anpassbarer Rückgabewert für die Funktion, wenn der angegebene Pfad ungültig ist. Wenn undefiniert gelassen, gibt die Funktion einen null-Wert zurück, wenn ein Problem mit dem angegebenen Pfad vorliegt.

Beschreibung

Ruft Daten aus einem JSON-Objekt-String unter Verwendung des angegebenen Pfades ab. Wenn der Pfad ungültig ist, wird ein anpassbarer Wert zurückgegeben.

Wichtig

Diese Funktion erfordert die Agent-Version 11.59 / 12.3 oder höher.

Beispiele

// Define the JSON object string:
json_string = '{ "company": [{ "name": "Jitterbit", "product": [{ "type": "iPaaS", "name": "Jitterbit iPaaS" },{ "type": "EDI", "name": "Jitterbit EDI" }] }] }';

GetJSONStringEx(json_string, "/company/[0]/product/[1]/name");
// Returns "Jitterbit EDI"

GetJSONStringEx(json_string, "/company/[0]/product");
// Returns '[{"type":"iPaaS","name":"Jitterbit iPaaS"},{"type":"EDI","name":"Jitterbit EDI"}]'

GetJSONStringEx(json_string, "/company/[0]/employees");
// Returns null

GetJSONStringEx(json_string, "/company/[0]/employees", "Not a valid path.");
// Returns "Not a valid path."

GetJSONStringEx(json_string, "/company/[1]");
// Returns null

GetJSONStringEx(json_string, "/company/[1]", "Out of range.");
// Returns "Out of range."

JSONParser

Deklaration

dictionary JSONParser(string json_string)

Syntax

JSONParser(<json_string>)

Erforderliche Parameter

  • json_string: Ein JSON-Objekt-String, der in ein JSON-Objekt umgewandelt werden soll.

Beschreibung

Wandelt einen JSON-Objekt-String in ein JSON-Objekt um.

Die maximale Größe des JSON-Objekt-Strings, der zur JSONParser-Funktion zu einem bestimmten Zeitpunkt übergeben werden kann, hängt von der Hardware und der Arbeitslast des Agenten in der Projektumgebung ab.

Wichtig

Diese Funktion erfordert die Agent-Version 11.29 oder höher.

Beispiele

// Define the JSON object string:
json_string = '{ "company": [{ "name": "Jitterbit", "product": [{ "type": "iPaaS", "name": "Jitterbit iPaaS" },{ "type": "EDI", "name": "Jitterbit EDI" }] }] }';

// Convert the JSON object string into a JSON object:
json_object = JSONParser(json_string);

result = json_object["company"][0]["product"][1]["name"];
// Equals "Jitterbit EDI"

result = json_object["company"][0]["product"];
// Equals {"[name=>""Jitterbit iPaaS"",type=>""iPaaS""]","[name=>""Jitterbit EDI"",type=>""EDI""]"}

JSONStringify

Deklaration

string JSONStringify(dictionary json_object)

Syntax

JSONStringify(<json_object>)

Erforderliche Parameter

  • json_object: Ein JSON-Objekt, das in einen JSON-Objekt-String umgewandelt werden soll.

Beschreibung

Wandelt ein JSON-Objekt in einen JSON-Objekt-String um.

Wichtig

Diese Funktion erfordert die Agent-Version 11.30 oder höher. Die Unterstützung für reservierte JSON-Zeichen wie " und \ sowie die Fähigkeit, null-Daten zu verarbeiten, erfordert die Agent-Version 11.45 oder höher.

Beispiel

// Define the JSON object structure:
json_object = Dict();
json_object["company"][0] = Dict();
json_object["company"][0]["name"] = "Jitterbit";
json_object["company"][0]["product"][0] = Dict();
json_object["company"][0]["product"][0]["type"] = "iPaaS";
json_object["company"][0]["product"][0]["name"] = "Jitterbit iPaaS";
json_object["company"][0]["product"][0]["active"] = True;
json_object["company"][0]["product"][0]["internalID"] = 123;
json_object["company"][0]["product"][0]["description"] = 'Low-code integration solutions that deliver enterprise-grade performance.\nSecure, scalable, and AI-infused, Jitterbit iPaaS grows with your organization.\n"Request a free trial today!"';
json_object["company"][0]["product"][1] = Dict();
json_object["company"][0]["product"][1]["type"] = "EDI";
json_object["company"][0]["product"][1]["name"] = "Jitterbit EDI";
json_object["company"][0]["product"][1]["active"] = True;
json_object["company"][0]["product"][1]["internalID"] = 124;
json_object["company"][0]["product"][1]["description"] = null();

JSONStringify(json_object);
// Returns '{"company":[{"name":"Jitterbit", "product":[{"name":"Jitterbit iPaaS", "type":"iPaaS", "active":1, "internalID":123, "description":"Low-code integration solutions that deliver enterprise-grade performance.\\nSecure, scalable, and AI-infused, Jitterbit iPaaS grows with your organization.\\n\\"Request a free trial today!\\""},
            {"name":"Jitterbit EDI", "type":"EDI", "active":1, "internalID":124, "description":null}]}]}'