Funções JSON no Jitterbit Studio
Introdução
JavaScript Object Notation (JSON) permite a manipulação de dados no formato JSON. Para obter mais informações sobre JSON, consulte IETF RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format.
GetJSONString
Declaração
string GetJSONString(string json_string, string path)
Sintaxe
GetJSONString(<json_string>, <path>)
Parâmetros obrigatórios
json_string: Uma string de objeto JSON da qual analisar dados.path: Um caminho válido representando o local dos dados na string de objeto JSON.
Descrição
Recupera dados de uma string de objeto JSON usando o caminho fornecido. Se o caminho for inválido, o fluxo de execução é interrompido e uma mensagem de erro é retornada.
Para solução de problemas, consulte GetJSONString: Execução interrompida em caminho inválido no guia de solução de problemas de operação.
Importante
Uma versão aprimorada dessa função, GetJSONStringEx, oferece melhores capacidades de tratamento de erros, incluindo controle sobre a mensagem retornada quando há um problema com o caminho fornecido.
Essa função requer agente versão 11.28 ou posterior.
Exemplos
// 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
Declaração
string GetJSONStringEx(string json_string, string path[, string value_if_no_match])
Sintaxe
GetJSONStringEx(<json_string>, <path>[, <value_if_no_match>])
Parâmetros obrigatórios
json_string: Uma string de objeto JSON da qual analisar dados.path: Um caminho válido representando o local dos dados na string de objeto JSON.
Parâmetros opcionais
value_if_no_match: Um valor de retorno personalizável para a função se o caminho fornecido for inválido. Se não for definido, a função retorna um valornullquando há um problema com o caminho fornecido.
Descrição
Recupera dados de uma string de objeto JSON usando o caminho fornecido. Se o caminho for inválido, um valor personalizável é retornado.
Importante
Essa função requer agente versão 11.59 / 12.3 ou posterior.
Exemplos
// 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
Declaração
dictionary JSONParser(string json_string)
Sintaxe
JSONParser(<json_string>)
Parâmetros obrigatórios
json_string: Uma string de objeto JSON a ser convertida em um objeto JSON.
Descrição
Converte uma string de objeto JSON em um objeto JSON.
O tamanho máximo da string de objeto JSON que pode ser passada para a função JSONParser em um determinado momento depende do hardware do agente e da carga de trabalho do ambiente do projeto.
Importante
Essa função requer agente versão 11.29 ou posterior. Preservar valores null em campos de objeto e elementos de matriz requer agente versão 12.12 ou posterior.
Exemplos
// 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
Declaração
string JSONStringify(dictionary json_object)
Sintaxe
JSONStringify(<json_object>)
Parâmetros obrigatórios
json_object: Um objeto JSON para converter em uma sequência de caracteres de objeto JSON.
Descrição
Converte um objeto JSON em uma sequência de caracteres de objeto JSON.
Importante
Esta função requer versão do agente 11.30 ou posterior.
- O suporte para caracteres JSON reservados, como
"e\, e a capacidade de lidar com dados nulos requer versão do agente 11.45 ou posterior. - O suporte para valores numéricos e booleanos requer versão do agente 12.12 ou posterior.
Exemplo
// 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":true, "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":true, "internalID":124, "description":null}]}]}'