Asim
Asim

Reputation: 976

JSON object insert in PHP

I'm having some issues with inserting some data in JSON object the right way. I want the JSON object to NOT have any keys on top of the object. Illustrating like this is my JSON now:

JSON NOW:

{
    "Account2": [{
        "accountId": "",
        "containerId": "",
        "tag": 1,
        "trigger": 1,
        "variable": 4,
        "account_name": "Account2"
    }, {
        "accountId": "",
        "containerId": "",
        "missing_live": true
    }],
    "Account 1": [{
        "accountId": "",
        "containerId": "",
        "tag": 2,
        "trigger": 1,
        "variable": 1,
        "account_name": "Account 1"
    }],
    "GTMdocx": [{
        "accountId": "",
        "containerId": "",
        "tag": 0,
        "trigger": 0,
        "variable": 1,
        "account_name": "GTMdocx"
    }]
}

As you can see "Account2", "Account 1", "GTMdocx", etc.. they are like account_name inside the json object. I would want to remove the "top"-key and keep the "account_name" instead. but when i remove the line in the code i only get one response(one JSON object) not all 3.

PHP code:

       try { // Some containers might not have live-version
            $container_version = self::listAccountsContainersVersion($account_container['path']);
            $account_container->tag = count($container_version['tag']);
            $account_container->trigger = count($container_version['trigger']);
            $account_container->variable = count($container_version['variable']);
            $account_container->account_name = $account['name'];

            // Add the whole account to the main container
            $containers[$account['name']][] = $account_container;

        } catch (\Exception $e) {
            $account_container->missing_live = true;
            $containers[$account['name']][] = $account_container;

            //$containers[$account['name']]['missing_live_version'] = $account_container; //Container not published
        }

ive commented // Add the whole account to the main container... here is the line where all get wrong. Ive tried to change it to:

$containers[$account] = $account_container;

So I will get all the $account JSON object included in $account_container but if I don't have $account['name'] the response show only one object when its 3. (this is inside a foreach loop).

So what i would want one JSON object to look like is this:

{
    "1": {
        "accountId": "",
        "containerId": "",
        "tag": 1,
        "trigger": 1,
        "variable": 4,
        "account_name": "Account2"
    },
    "2": {
        "accountId": "",
        "containerId": "",
        "missing_live": true,
        "account_name": "Account2"
    },
    "3": {
        "accountId": "",
        "containerId": "",
        "tag": 2,
        "trigger": 1,
        "variable": 1,
        "account_name": "Account 1"
    },
    "4": {
        "accountId": "",
        "containerId": "",
        "tag": 0,
        "trigger": 0,
        "variable": 1,
        "account_name": "GTMdocx"
    },
    "5": {
        "accountId": "",
        "containerId": "",
        "tag": 0,
        "trigger": 0,
        "variable": 0,
        "account_name": "Account3"
    }
}

You can see I removed the "Account2" on top.

Upvotes: 0

Views: 67

Answers (1)

shribalaji ganesan
shribalaji ganesan

Reputation: 26

So to get your required JSON string you need to have a single dimensions array

so save your account_container like this

$containers[] = $account_container;

Upvotes: 1

Related Questions