Working with JSON data in Nashorn

Nashorn is a JavaScript engine that runs on the Java Virtual Machine, allowing us to execute JavaScript code from within Java applications. One common use case for Nashorn is working with JSON data. In this article, we will explore how to work with JSON data in Nashorn.

Table of Contents

  1. Parsing JSON
  2. Accessing JSON properties
  3. Modifying JSON data
  4. Stringifying JSON

Parsing JSON

To parse JSON data in Nashorn, we can use the JSON.parse() function. This function takes a JSON string as input and returns a JavaScript object representing the parsed JSON data.

var jsonString = '{"name": "John", "age": 30, "city": "New York"}';
var jsonObject = JSON.parse(jsonString);

print(jsonObject.name); // Output: John
print(jsonObject.age); // Output: 30
print(jsonObject.city); // Output: New York

Accessing JSON properties

Once we have parsed the JSON data into a JavaScript object, we can access its properties using the dot notation or square bracket notation.

var jsonString = '{"name": "John", "age": 30, "city": "New York"}';
var jsonObject = JSON.parse(jsonString);

var name = jsonObject.name;
print(name); // Output: John

var age = jsonObject['age'];
print(age); // Output: 30

Modifying JSON data

To modify JSON data in Nashorn, we can simply assign new values to the properties of the JavaScript object representing the JSON data.

var jsonString = '{"name": "John", "age": 30, "city": "New York"}';
var jsonObject = JSON.parse(jsonString);

jsonObject.age = 31;
jsonObject.city = "San Francisco";

var modifiedJsonString = JSON.stringify(jsonObject);
print(modifiedJsonString); // Output: {"name":"John","age":31,"city":"San Francisco"}

Stringifying JSON

To convert a JavaScript object back to a JSON string, we can use the JSON.stringify() function. This function takes a JavaScript object as input and returns a JSON string representing the object.

var jsonObject = {
  "name": "John",
  "age": 30,
  "city": "New York"
};

var jsonString = JSON.stringify(jsonObject);
print(jsonString); // Output: {"name":"John","age":30,"city":"New York"}

In conclusion, working with JSON data in Nashorn is quite straightforward. We can easily parse JSON strings into JavaScript objects, access and modify properties, and convert them back to JSON strings when needed. This makes Nashorn a powerful tool for working with JSON data within Java applications.

#hashtags: JSON, Nashorn