B2Win Suite Documentation (Under Construction)
5.4
5.4
  • B2Win Suite Documentation
  • Scripting
    • B2Win Suite Scripting Overview
    • Scripting in B2Data
      • Custom Script Nodes
        • Custom Script
        • Source Script Node (File)
        • Source Script Node (Table)
        • Source Script Node (Object)
        • If-Else Condition
        • Condition Node
      • In DataPrep
      • Properties
      • Workflow Applications
        • Workflow #1
        • Workflow #2
    • Getting Started
    • Language Basics
    • Types & Objects
      • Primitives
      • Objects
        • Instant
        • LocalDateTime
        • LocalDate
        • LocalTime
        • JsonNode
        • ObjectNode
        • File
        • SuiteTable
        • Duration
      • Arrays & Maps
      • Row, Column, StringColumn
    • Utilities
      • Date & Time
        • InstantUtil
        • GeneralDateUtil
        • LocalDateTimeUtil
        • LocalDateUtil
        • LocalTimeUtil
        • Formatting
        • Timezones
      • Math
      • StrictMath
      • NumericUtil
      • DbUtil
      • DbExecutor
      • StateUtil
      • FileUtil
      • JsonUtil
      • HttpUtil
      • NodeInputReader
      • RandomUtil
      • HashingUtil
      • CompressionUtil
      • Logging
    • Properties
    • Context and System
      • Context
      • System
    • Configurations
      • Workflow Configuration
    • Tutorial
    • Feedback
  • B2Data
    • Workflow Settings
      • General Settings
      • Global/All Properties
      • Execution Configuration
        • General
        • Runtime
        • Compiler
        • Storage
        • Clean Up
      • Permissions
    • Nodes
      • Scheduler
    • Services Configuration
      • Power BI
Powered by GitBook
On this page
  • Methods
  • Examples
  1. Scripting
  2. Utilities

JsonUtil

The JsonUtil class provides utility methods for working with JSON data. It includes methods for converting objects to JSON, parsing JSON strings, and manipulating JSON data.

Methods

toJson(object: any): JsonNode

Converts a JavaScript object to a JSON representation and returns a JsonNode object.

// Convert an object to JSON
var dataObject = { "name": "John", "age": 30 };
var jsonData = JsonUtil.toJson(dataObject);

parse(jsonStr: String): JsonNode

Parses a JSON string and returns a JsonNode object representing the parsed JSON data.

// Parse a JSON string
var jsonString = '{"name": "Alice", "age": 25}';
var parsedData = JsonUtil.parse(jsonString);

stringify(jsonNode: JsonNode): String

Converts a JsonNode object back to a JSON string.

// Stringify a JsonNode
var jsonString = JsonUtil.stringify(jsonData);

prettyPrint(jsonNode: JsonNode): String

Formats and pretty-prints a JsonNode object as a JSON string with proper indentation and line breaks.

// Pretty print a JsonNode
var prettyJsonString = JsonUtil.prettyPrint(parsedData);


readPath(jsonNode: String, path: String): any
Reads a specific value from a JSON object using a JSON path and returns it.
// Read a value from JSON using a pathvar jsonContent = '{"person": {"name": "Bob", "address": {"city": "New York"}}}';var city = JsonUtil.readPath(jsonContent, "person.address.city");return "City: " + city; // City: New York

readPath(jsonNode: String, path: String): any

Reads a specific value from a JSON object using a JSON path and returns it.

// Read a value from JSON using a path
var jsonContent = '{"person": {"name": "Bob", "address": {"city": "New York"}}}';
var city = JsonUtil.readPath(jsonContent, "person.address.city");
return "City: " + city; // City: New York

createObjectNode(): ObjectNode

Create an empty Json Object to be set with values.

// Create an empty Json Object
var objectNode = JsonUtil.createObjectNode();
// Using the object
objectNode.put("doubleKey", Double.parseDouble("553.1"));
objectNode.put("str", "This is a string");
objectNode.put("int", Integer.sum(4, 5));
objectNode.set("jsonObject", JsonUtil.toJson(Instant.now()));
return objectNode;

Examples

Example: JSON Read Using JSON Path

Description: This script reads a JSON property value using JSON path from a JSON string.

Use Case: Useful for extracting specific data from structured JSON objects.

// Reads the signature filename from a json string
var jsonString = '{"signature":{"fileName":"mySignature.jpg"}}';
var signatureFileName = JsonUtil.readPath(jsonString, "$.signature.fileName");
PreviousFileUtilNextHttpUtil

Last updated 1 year ago