> For the complete documentation index, see [llms.txt](https://docsv1.polyfrost.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docsv1.polyfrost.org/utilities-module/json-utilities.md).

# JSON utilities

## Getting JSON elements from a string

### Unsafe (exceptional) parsing

You can **unsafely (exceptionally)** parse JSON from a string using `JsonUtils.parse(String)`, like so:

```java
JsonElement jsonElement = JsonUtils.parse("{}");
```

If incorrect JSON syntax is passed to this method, GSON (the underlying JSON library) will throw a `JsonSyntaxException`

### Safe parsing

`JsonUtils.parseOrNull` will return the resulting `JsonElement`, or null in case any json errors are encountered.

```java
JsonElement jsonElement1 = JsonUtils.parseOrNull("{}"); // non-null, JsonObject
JsonElement jsonElement2 = JsonUtils.parseOrNull("Hello, OneConfig!"); // null
```

### Safe parsing via callbacks

An additional, method exists for the purpose of running a callback **if** parsing succeeds on the given input.

```java
JsonUtils.parse("{}", (jsonElement) -> {
    System.out.println("I'll print out if the JSON is parsed properly! " + jsonElement);
});
```


---

# 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://docsv1.polyfrost.org/utilities-module/json-utilities.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.
