# How to Work with the PxPlus JSON Object

> Source: https://manual.pvxplus.com/how-to/how-to-work-with-json-object

Starting with PxPlus 2025, a new **[JSON Object](https://manual.pvxplus.com/utilities/obj-json)** (\*obj/json) has been added. It introduces a richer set of features and methods, greatly simplifying the creation and manipulation of JSON data while significantly improving current JSON functionality and performance. The object program ***json.pvc*** is found in the ***\*lib/obj*** directory.

In conjunction with changes made to the **[XML Object](https://manual.pvxplus.com/utilities/obj-xml)** (\*obj/xml), XML data can also be easily converted to JSON (and vice versa) using the objects.

See **[How to Convert XML to JSON](https://manual.pvxplus.com/how-to/how-to-convert-xml-to-json)** and **[How to Convert JSON to XML Using the XML Object](https://manual.pvxplus.com/how-to/how-to-convert-json-to-xml)**.

Note:  
The JSON object is also compatible with Booleans, null values, and null lists. Keys are case sensitive.

For the example below, you will be working with order details information. You will:

- Create order details information.
- Create customer details and include the order.
- Update location information in various ways.
- Save the JSON data.

These steps will show you some of the functions that involve working with order information using the JSON object. At any time, you can use the Print method **json_obj'print( )** to see the order details in the file up to that point.

## How to Create an Object

Instantiate an object called ***order***.

```
json_obj = NEW("*obj/json")
```

***Example:***

```
order=new("*obj/json")
```

## How to Add Order Details Information

Using the **Append( )** method, add the *key_name* (with its numeric/string *value*) to the object. If the key already exists, an Error #11 (Record not found or Duplicate key on write) will occur.

```
json_obj'append("key_name", "value")
```

***Example:***

```
order'append("ID",12345)
order'append("Name","Apple")
order'append("Amount",10)
order'append("Unit Price",1.85)
```

## How to Create Customer Details

These steps show you how to create the customer details:

|  |  |  |
| --- | --- | --- |
|  | 1\. | Instantiate another object called ***order_info***.<br>***Example:***<br>order_info=new("\*obj/json") |
|  | 2\. | Input for the *key* and *value* for the Customer Name.<br>***Example:***<br>order_info'append("Customer Name","John Doe") |
|  | 3\. | Input for the *key* and *value* for the Date of expected delivery.<br>***Example:***<br>order_info'append("Date","01/01/2025")<br>Using the **Append_to(** **)** method, append a *key1:string* to a *key_name*. If the key already exists, an Error #11 (Record not found or Duplicate key on write) will occur.<br>***Example:***<br>append_to(key$,key2$,value$) |
|  | 4\. | Input for the *key* and *value* for the Country.<br>***Example 1:***   <br>  <br> order_info'append_to("Location","Country","Canada")  <br>  <br>***Example 2:***  <br>  <br> order_info'append_to("Location.Country","Canada") |
|  | 5\. | Input for the *key* and *value* for the Province.<br>***Example 1:***  <br>  <br> order_info'append_to("Location","Province","Ontario")  <br>  <br>***Example 2:***  <br>  <br> order_info'append_to("Location.Province","Ontario")<br>Note:  <br>Dot Notation is used in *Example 2* for Step 4 and Step 5 above. |
|  | 6\. | Using the **Append_json_to( )** method, you can append any other JSON object to a *key_name*. If the key already exists, an Error #11 (Record not found or Duplicate key on write) will occur.<br>Input for the ***order*** object into the ***order_info*** object.<br>***Example:***<br>order_info'append_json_to("Order",order) |

## How to Drop an Object

Now that the ***order*** object has been added, drop the ***order*** object.

***Example:***

drop object order

## How to Perform Calculations

Using the **Get_num(** **)** method, calculate the total order amount.

***Example:***

```
total=order_info'get_num("Order.Amount")*order_info'get_num("Order.Unit Price")
```

## How to Save the Totals from a Calculation

Using the **Set( )** method, set a *key_name* with its *value*.

***Example:***

```
order_info'set("Total",total)
```

## How to Save a JSON File

Using the **Save_json_file(** **)** method, save the file using a full path.

```
json_obj'save_json_file("<fullpath_tofile.json>")
```

***Example:***

```
order_info'save_json_file("orders.json")
```

## How to View the JSON Data for the Order

Using the **Print( )** method, you can print the data.

***Example:***

```
order_info'print()
```

![](https://manual.pvxplus.com/migration-assets/bc39302e3c58312ac9e7f65b51d54019b733eb1363f001854481ebba961638be.png)

## How to Modify the Data

Remove all Location data from ***order_info***.

***Example:***

```
order_info'remove("Location")
```

![](https://manual.pvxplus.com/migration-assets/1dce9515e18325dc1690686607fb93233e6ab32d3082478a9bbd54e0711b435d.png)

Only remove Location:Address data from ***order_info***.

***Example:***

```
order_info'remove("Location.Address")
```

![](https://manual.pvxplus.com/migration-assets/1533b89c8f915db76d45fa9967aad834a54499d0f8018a17712315079bd92bcf.png)

## How to Load JSON Files

Using the **Load_json_file(** **)** method, load the file using a full path.

```
json_obj'load_json_file("<fullpath_tofile.json>")
```

***Example:***

```
order_info'load_json_file("orders.json")
```

## How to Load JSON Data

***How to Load the Data from an Array***

These steps show you how to load the data from an array.

|  |  |  |
| --- | --- | --- |
|  | 1\. | Create an associative array.<br>Note:  <br>When assigning a numeric as a string (such as StreetNum in the example below), associative arrays know that it is a numeric; therefore, when printed, it prints numeric.<br>***Example:***<br>```<br>Dim my_array$<br>my_array$["FirstName"]="Jane"<br>my_array$["LastName"]="Smith"<br>my_array$["StreetNum"]="456"<br>my_array$["Address"]="Ontario Street"<br>``` |
|  | 2\. | Create an object called ***json_obj***.<br>***Example:***<br>json_obj=new("\*obj/json") |
|  | 3\. | Using the **Load_array( )** method, load the data from the array.<br>***Example:***<br>json_obj'load_array(my_array${all})<br>Using the **Print( )** method, you can print the JSON data.<br>***Example:***<br>json_obj'print()<br>![](https://manual.pvxplus.com/migration-assets/209a5292c8f487b214d83b303f9a46008e9e7574a4193b71075e20497d1647b1.png) |

***How to Load the Data from a String***

These steps show you how to load the data from a string.

|  |  |  |
| --- | --- | --- |
|  | 1\. | Set up a string.<br>***Example:***<br>string$="{Address: ""Ontario"",StreetNum: 456}" |
|  | 2\. | Load the data from a string.<br>***Example:***<br>json_obj'load_string(string$) |
|  | 3\. | Using the **Print( )** method, you can print the JSON data.<br>***Example:***<br>json_obj'print()<br>![](https://manual.pvxplus.com/migration-assets/c127a6d7ffec1049b8f1b361ecb518b323334b769926544bd1c479f3c271f0b3.png)<br>Note:   <br>Reusing load_string without closing the object will clear the previous data. |

## How to Work with a List

These steps show you how to work with a list.

|  |  |  |
| --- | --- | --- |
|  | 1\. | Create an object called ***json_list***.<br>***Example:***<br>json_list=new("\*obj/json") |
|  | 2\. | Using the **Append( )** method, add the *key_name* with its numeric/string *value* to the object. If the key already exists, an Error #11 (Record not found or Duplicate key on write) will occur.<br>json_obj'append("key_name","value")<br>***Example 1:***  <br>  <br> json_list'append("1",1)  <br>  <br>***Example 2***:  <br>  <br> json_list'append("2",2) |
|  | 3\. | Using the **Append_to( )** method, append a third index to the array.<br>json_list'append_to("3.Location","1","value")<br>***Example:***<br>json_list'append_to("3.Loc_2","1","Value_3")<br>***OR***<br>json_list'append_to("3.Key","2","value_2")<br>***Example:***<br>json_list'append_to("3.Location","2","abc") |
|  | 4\. | Using the **Print( )** method, you can see the data.<br>***Example:***<br>json_list'print() |
|  | 5\. | Drop the json_obj objects.<br>***Example:***<br>drop object json_list |

## See Also

**[\*OBJ/JSON - JSON Object](https://manual.pvxplus.com/utilities/obj-json)  
[\*OBJ/XML - XML Object](https://manual.pvxplus.com/utilities/obj-xml)  
[Associative Arrays (Hash Tables)](https://manual.pvxplus.com/pxplus-user-guide/language-elements/data-types-literals-and-variables/associative-arrays-hash-tables)**  
**[DIM Define Arrays and Strings](https://manual.pvxplus.com/directives/dim)**  
**[DIM( ) Generate String/Get Array Size](https://manual.pvxplus.com/functions/dim)**  
**['JV' JSON Version](https://manual.pvxplus.com/parameters/jv)**
