3

I have been struggling with the following code to make it useable, but I don't know what type of data it is.

This part is extracted from a payload posted by ERPNext to my webhook. I have drilled down to specific part of the data, but it seems mix of different data types.

Array
(
    [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
        (
            [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
        )

)

Following was the original json decoded data from payload

Array
(
    [data] => 
    [{"items": ] => Array
        (
            [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
                (
                    [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
                )

        )

    [headers] => Array
        (
        )

)

I am looking forward to something clean like

Array
(
    [stock_qty] => 131.0
    [base_price_list_rate] => 4500.0
    [creation] => 2019-04-02 22:33:56.356904
    [base_amount] => 589500.0
)

2 Answers2

2

This is kinda weird, you would have to stitch together the json (which is inside keys as well as values)

Try the following function it iterates through the decoded array, then detects the start of the data {, stitches together the json, and passes it through json decode a second time.

function decodeweird($data,$sub=false) {
    $return = '';
    foreach($data as $key => $value) {
        //detect the beginning of the data, or just append if we've already started
        if($sub || substr($key,0,1) == '{') {
            $return .= $key;
            if(!empty($value) && is_array($value)) $return .= decodeweird($value,true);
        }
    }
    return $sub ? $return:json_decode($return,true);
}

$received_data = <<<EOD
{
  "{\"items\": ": {
    "{\"stock_qty\": 131.0, \"base_price_list_rate\": 4500.0, \"image\": \"\", \"creation\": \"2019-04-02 22:33:56.356904\", \"base_amount\": 589500.0, \"qty\": 131.0, \"margin_rate_or_amount\": 0.0, \"rate\": 4500.0, \"total_weight\": 0.0, \"ensure_delivery_based_on_produced_serial_no\": 0, \"owner\": \"Administrator\", \"target_warehouse\": null, \"stock_uom\": \"Nos\", \"base_net_amount\": 589500.0, \"page_break\": 0, \"modified_by\": \"Administrator\", \"base_net_rate\": 4500.0, \"discount_percentage\": 0.0, \"item_name\": \"5277\", \"amount\": 589500.0, \"actual_qty\": 0.0, \"net_rate\": 4500.0, \"conversion_factor\": 1.0, \"base_rate_with_margin\": 0.0, \"supplier\": null, \"docstatus\": 1, \"prevdoc_docname\": null, \"uom\": \"Nos\", \"ordered_qty\": 0.0, \"doctype\": \"Sales Order Item\", \"description\": \"5277\", \"parent\": \"SAL-ORD-2019-00019\", \"gross_profit\": 589500.0, \"returned_qty\": 0.0, \"brand\": null, \"base_rate\": 4500.0, \"item_code\": \"5277\", \"produced_qty\": 0.0, \"projected_qty\": -453.0, \"warehouse\": \"Stores - AFSPK\", \"margin_type\": \"\", \"billed_amt\": 0.0, \"rate_with_margin\": 0.0, \"pricing_rule\": null, \"delivered_qty\": 0.0, \"delivered_by_supplier\": 0, \"discount_amount\": 0.0, \"price_list_rate\": 4500.0, \"weight_uom\": null, \"transaction_date\": \"2019-04-02\", \"name\": \"f94099637a\", \"idx\": 1, \"item_tax_rate\": \"{}\", \"item_group\": \"Products\", \"planned_qty\": 0.0, \"modified\": \"2019-04-02 22:34:00.282021\", \"weight_per_unit\": 0.0, \"work_order_qty\": 0.0, \"parenttype\": \"Sales Order\", \"customer_item_code\": null, \"blanket_order_rate\": 0.0, \"valuation_rate\": 0.0, \"net_amount\": 589500.0, \"blanket_order\": null, \"delivery_date\": \"2019-06-28\", \"parentfield\": \"items\"}": {
      ", \"customer_address\": null, \"customer_name\": \"Muhammad\", \"name\": \"SAL-ORD-2019-00019\", \"title\": \"Muhammad\"}": null
    }
  },
  "headers": {
  }
}
EOD;
$received = json_decode($received_data,true);

var_dump(decodeweird($received));
Valerie
  • 332
  • 2
  • 9
  • Hi @Valerie, I have another wierd Json data received from the same software. It's a bit different and above function return null. Can you please help? – Muhammad Ashffaq Jun 15 '19 at 12:53
0

It looks like you have json arrays, just pass it to json_decode.

ex:

$jsonData = "{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}"

$assosArray = json_decode($jsonData, true);

Note the true, it has to be set to true to be an associative array!

Lulceltech
  • 1,662
  • 11
  • 22