0

I'm trying to process a Magento object with JSON. Using json_encode($order->getData()) or Mage::helper('core')->jsonEncode($order->getData()), I get output such as the following:

{ "entity_id": "1", "state": "complete", "status": "complete", "coupon_code": null, "protect_code": "30447e", "shipping_description": "U.S. Postal Service - First Class", "is_virtual": "0", "store_id": "1", "customer_id": null, "base_discount_amount": "0.0000", "base_discount_canceled": "0.0000", "base_discount_invoiced": "0.0000", "base_discount_refunded": null, "base_grand_total": "8.6500", "base_shipping_amount": "3.3000", "base_shipping_canceled": null, "base_shipping_invoiced": "3.3000", "base_shipping_refunded": null, "base_shipping_tax_amount": "0.0000", "base_shipping_tax_refunded": null, "base_subtotal": "5.3500", "base_subtotal_canceled": null, "base_subtotal_invoiced": "5.3500", "base_subtotal_refunded": null, "base_tax_amount": "0.0000", "base_tax_canceled": null, "base_tax_invoiced": "0.0000", "base_tax_refunded": null, "base_to_global_rate": "1.0000", "base_to_order_rate": "1.0000", "base_total_canceled": null, "base_total_invoiced": "8.6500", "base_total_invoiced_cost": null, "base_total_offline_refunded": "0.0000", "base_total_online_refunded": "0.0000", "base_total_paid": "8.6500", "base_total_qty_ordered": "0.0000", "base_total_refunded": "0.0000", "discount_amount": "0.0000", "discount_canceled": "0.0000", "discount_invoiced": "0.0000", "discount_refunded": null, "grand_total": "8.6500", "shipping_amount": "3.3000", "shipping_canceled": null, "shipping_invoiced": "3.3000", "shipping_refunded": null, "shipping_tax_amount": "0.0000", "shipping_tax_refunded": null, "store_to_base_rate": "1.0000", "store_to_order_rate": "1.0000", "subtotal": "5.3500", "subtotal_canceled": null, "subtotal_invoiced": "5.3500", "subtotal_refunded": null, "tax_amount": "0.0000", "tax_canceled": null, "tax_invoiced": "0.0000", "tax_refunded": null, "total_canceled": null, "total_invoiced": "8.6500", "total_offline_refunded": "0.0000", "total_online_refunded": "0.0000", "total_paid": "8.6500", "total_qty_ordered": "1.0000", "total_refunded": "0.0000", "can_ship_partially": null, "can_ship_partially_item": null, "customer_is_guest": "1", "customer_note_notify": "1", "billing_address_id": "1", "customer_group_id": "0", "edit_increment": null, "email_sent": "1", "forced_shipment_with_invoice": null, "gift_message_id": null, "payment_auth_expiration": null, "paypal_ipn_customer_notified": null, "quote_address_id": null, "quote_id": "8", "shipping_address_id": "2", "adjustment_negative": null, "adjustment_positive": null, "base_adjustment_negative": null, "base_adjustment_positive": null, "base_shipping_discount_amount": null, "base_subtotal_incl_tax": null, "base_total_due": null, "payment_authorization_amount": null, "shipping_discount_amount": null, "subtotal_incl_tax": null, "total_due": null, "weight": "0.2200", "customer_dob": null, "increment_id": "100000002", "applied_rule_ids": "", "base_currency_code": "USD", "customer_email": "[email protected]", "customer_firstname": null, "customer_lastname": null, "customer_middlename": null, "customer_prefix": null, "customer_suffix": null, "customer_taxvat": null, "discount_description": null, "ext_customer_id": null, "ext_order_id": null, "global_currency_code": "USD", "hold_before_state": null, "hold_before_status": null, "order_currency_code": "USD", "original_increment_id": null, "relation_child_id": null, "relation_child_real_id": null, "relation_parent_id": null, "relation_parent_real_id": null, "remote_ip": "63.230.211.89", "shipping_method": "tablerate_bestway", "store_currency_code": "USD", "store_name": "Main Website\nStore\nDefault Store View", "x_forwarded_for": null, "customer_note": null, "created_at": "2009-05-15 23:14:53", "updated_at": "2009-05-16 15:01:29", "total_item_count": "1", "customer_gender": null, "base_custbalance_amount": null, "custbalance_amount": null, "is_multi_payment": null, "hidden_tax_amount": null, "base_hidden_tax_amount": null, "shipping_hidden_tax_amount": null, "base_shipping_hidden_tax_amnt": null, "hidden_tax_invoiced": null, "base_hidden_tax_invoiced": null, "hidden_tax_refunded": null, "base_hidden_tax_refunded": null, "shipping_incl_tax": null, "base_shipping_incl_tax": null, "coupon_rule_name": null, "auctaneapi_discounts": null, "payment_authorization_expiration": null, "forced_do_shipment_with_invoice": null, "base_shipping_hidden_tax_amount": null }

Note how integers, decimals and currency are quoted. This isn't proper JSON-formatting as it processes as a string rather than a number. Anyone know how to modify this either with custom options or a special function that converts these to non-quoted numbers instead of strings?

2 Answers 2

2

From PHP 5.3.3 on there is a JSON constant JSON_NUMERIC_CHECKthat you can use when calling json_encode():

json_encode($order->getData(), JSON_NUMERIC_CHECK );

I saw that there can also be $options passed to Mage::helper('core')->jsonEncode($order->getData(), $cycleCheck, $options); but from Zend_Json it seems this is only used for enableJsonExprFinder as option ($options['enableJsonExprFinder']) so I think going with the plain json_encode function is better in this case.

2
  • Thanks, I was thinking about using this but wanted compatibility with PHP 5.2 if possible :( Mar 12, 2015 at 13:33
  • Uh 5.2 compatibility... Well, then :) Mar 12, 2015 at 14:03
2

wound up using this... does the trick as a helper preprocessor that then calls core jsonEncode without needing php 5.3.

foreach($o->getData() as $k=>$v ){ 
  if (is_numeric($v)){ 
    $o->setData($k,(float)$v); 
   } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.