[Solved] How would I build this JSON using a PHP array


The only way I know is to json encode an array like this:

<?php

echo json_encode(
    [
        "bundles" =>[ [
            "type" => "TYPE1",
            "items" => [
                [
                    "bom" => [
                        [
                            "type" => "C",
                            "stockId" => "1",
                            "quantity" => 1,
                            "metadata" => [
                                "key" => "value"
                            ]
                        ],
                        [
                            "type" => "E",
                            "quantity" => 1,
                            "stockId" => "2"
                        ]
                    ]
                ]
            ],
            "delivery" => [
                "method" => "FIRST_CLASS_ROYAL_MAIL",
                "shippingAddress" => [
                    "firstname" => "value",
                    "lastname" => "value",
                    "addressLine1" => "value",
                    "addressLine2" => "value",
                    "town" => "value",
                    "county" => "value",
                    "postcode" => "value"
                ]
            ]
        ],
        [
            "type" => "TYPE2",
            "items" => [
                [
                    "bom" => [
                        [
                            "type" => "C",
                            "stockId" => "4",
                            "quantity" => 1,
                            "metadata" => [
                                "key" => "value",
                                "key" => "value"
                            ]
                        ]
                    ]
                ]],
                "delivery" => [
                    "method" => "E",
                    "key" => "value",
                    "key" => "value"
                ]
            ]]
        ]
    );

That returns

{“bundles”:[{“type”:”TYPE1″,”items”:[{“bom”:[{“type”:”C”,”stockId”:”1″,”quantity”:1,”metadata”:{“key”:”value”}},{“type”:”E”,”quantity”:1,”stockId”:”2″}]}],”delivery”:{“method”:”FIRST_CLASS_ROYAL_MAIL”,”shippingAddress”:{“firstname”:”value”,”lastname”:”value”,”addressLine1″:”value”,”addressLine2″:”value”,”town”:”value”,”county”:”value”,”postcode”:”value”}}},{“type”:”TYPE2″,”items”:[{“bom”:[{“type”:”C”,”stockId”:”4″,”quantity”:1,”metadata”:{“key”:”value”}}]}],”delivery”:{“method”:”E”,”key”:”value”}}]}

That is exactly your json …

6

solved How would I build this JSON using a PHP array