// Creating a Varien_Object $obj = new Varien_Object(); $obj->setData('name', 'John Doe'); $obj->setData('age', 30); // Converting to JSON $jsonString = $obj->toJson(); echo $jsonString; // {"name":"John Doe","age":30}
// Creating a Varien_Object with an array of data $data = array( 'name' => 'Jane Doe', 'age' => 25 ); $obj = new Varien_Object($data); // Converting to JSON with options $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; $jsonString = $obj->toJson($options); echo $jsonString; /* { "name": "Jane Doe", "age": 25 } */In this example, we create a new Varien_Object using an array of data as the constructor argument. Then, we use the toJson() method with options to convert the object to a JSON formatted string with pretty printing and unescaped slashes. The package library used in these examples is the Magento PHP library, specifically the Varien_Object class.