> For the complete documentation index, see [llms.txt](https://docs.b2winsuite.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.b2winsuite.com/5.2/scripting/static-classes/jsonutil.md).

# 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.

```typescript
// 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.

```typescript
// 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.

```typescript
// 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.

```typescript
// 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.

```typescript
// 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
```

***

### 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.

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.b2winsuite.com/5.2/scripting/static-classes/jsonutil.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
