JSON

JSON Guide

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 and JSON(MDN). Reference documentation about JSON can be found here.

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() function. This method converts a JSON-formatted string into a JavaScript object.

Code Example:

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(), 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:

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.

/**
 * 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.

/**
 * 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);
}

Last updated