# JSON

## Overview

JSON (JavaScript Object Notation) is a lightweight, text-based format commonly used for data interchange. It is easy for humans to read and write and for machines to parse and generate. JSON is particularly popular for web applications and APIs due to its simplicity and compatibility with JavaScript.

For more details on the JSON standard, refer to [JSON.org](https://www.json.org/json-en.html) and [JSON(MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON).\
Reference documentation about JSON can be found [here](/reference/servoycore/dev-api/js-lib/json.md#overview).

Common Use Cases:

* **Serializing data to files**: Storing structured data in JSON format for easy retrieval.
* **Data exchange via web services**: JSON is widely used in RESTful APIs to send and receive data between client and server.

## Reading/Parsing JSON (text to object)

To parse JSON data, you can use JavaScript's built-in [JSON.parse()](/reference/servoycore/dev-api/js-lib/json.md#parse-text) function. This method converts a JSON-formatted string into a JavaScript object.

Code Example:

```javascript
var jsonString = '{"name": "Alice", "age": 30, "isMember": true}';
var parsedObject = JSON.parse(jsonString); // Parsing JSON to an object

application.output("Name: " + parsedObject.name); // Accessing object properties
application.output("Age: " + parsedObject.age);
```

## Writing/Generating JSON (object to text)

To generate JSON from a JavaScript object, use [JSON.stringify()](/reference/servoycore/dev-api/js-lib/json.md#stringify-value), which converts the object to a JSON-formatted string. This is useful when you need to store or transmit data in a structured format.

Code Example:

```javascript
var person = {
    name: "Alice",
    age: 30,
    isMember: true
};

var jsonString = JSON.stringify(person); // Converting the object to JSON
application.output(jsonString); // Outputting JSON string
```

## Examples

### Simple JSON Example (Converting Car Data to JSON)

This example demonstrates how to create a simple JavaScript object representing a car and convert it into JSON format.

```javascript
/**
 * Simple example: Convert car data to JSON
 * @properties={typeid:24,uuid:"E63276AA-79A1-47FC-B5BC-11E371E9A08D"}
 */
function objectToJSON(){
    var car = {};
    car.year = 2007;
    car.make = "Volvo";
    car.model = "XC90";
    car.color = "Silver";
    car.options = {
        interior: "leather",
        airbags: "curtain airbags",
        turbo: true
    };

    var json = JSON.stringify(car); // Converting car object to JSON
    application.output(json); // Outputting JSON representation
}
```

### Converting a JSRecord to JSON

This example takes an order record from the database, including related customer and employee details, and converts it to a JSON object. It then serializes the object to JSON format.

```javascript
/**
 * Writes the selected order record to JSON file format
 * @properties={typeid:24,uuid:"1784425B-31E3-4B32-8A7A-107CD155A554"}
 */
function orderRecordToJSON(){
    var orderObject = {};
    var order = foundset.getSelectedRecord();

    // Related customer
    orderObject.customerName = order.orders_to_customers.companyname;

    // Related employee
    orderObject.salesPerson = order.orders_to_employees.lastname;

    // Order shipping info
    orderObject.shipAddress = {
        address: order.shipaddress,
        city: order.shipcity,
        country: order.shipcountry
    };

    // Order Details
    orderObject.orderDetails = [];
    for (var i = 1; i <= order.orders_to_order_details.getSize(); i++) {
        var orderDetail = order.orders_to_order_details.getRecord(i);
        var orderDetailObject = {
            product: orderDetail.order_details_to_products.productname,
            quantity: orderDetail.quantity,
            unitPrice: orderDetail.unitprice
        };
        
        orderObject.orderDetails.push(orderDetailObject);
    }

    var json = JSON.stringify(orderObject); // Converting orderObject to JSON
    application.output(json);
    // Uncomment below line to save JSON to file
    // plugins.file.writeTXTFile('c:/temp/order.json', json);
}
```


---

# Agent Instructions: 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:

```
GET https://docs.servoy.com/guides/develop/programming-guide/working-with-files/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
