ThanhLam112358
ThanhLam112358

Reputation: 916

How to write content as JSON format

I have a trouble with JSON in PHP.

I have a JSON structure like this (with some line is const and some line is variable)

{
    "user": {
        "lam": {---->variable
            "\/lam\/files\/a": { ---->variable
                "id": 9, ---->variable
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498", ---->variable
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU=" ---->variable
                }
            }
        }
    }
}

I don't know how to write this content to file as JSON format like this. And in a number of case, I must write append to this file and result must like this:

{
    "user": {
        "lam": {
            "\/lam\/files\/a": {
                "id": 9,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU="
                }
            },
            "\/lam\/files\/test": {
                "id": 12,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-152153961597103330",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\/\/controller:5000\/v2.0",
                    "password_encrypted": "MjdzcDlrenptcG5udzI2MLSQvuGIczY\/SyHZVf9o7e8="
                }
            }
        }
    }
}

Upvotes: 2

Views: 72

Answers (2)

benyafai
benyafai

Reputation: 153

You'll need to work with the data as an array (or object) as PHP won't work directly in JSON;

Get your current data from the file into an array:

$data = json_decode(file_get_contents($pathtojsonfile, true));

Append the new data to the array:

$data[] = $newdata;

Then save the newly appended data back to file.

file_put_contents($pathtojsonfile, json_encode($data));

Upvotes: 0

Syscall
Syscall

Reputation: 19780

You could create an array, before to use json_encode():

Here is an example.

// prepare an array with variable (should come from database, or whatever)
$vars = [
  [
    'user' => 'lam',
    'path' => '/lam/files/a',
    'id' => 9,
    'bucket' => 'lam-15215156681752265498',
    'pass' => 'NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU=',
  ],
  [
    'user' => 'lam',
    'path' => '/lam/files/test',
    'id' => 12,
    'bucket' => 'lam-152153961597103330',
    'pass' => 'MjdzcDlrenptcG5udzI2MLSQvuGIczY/SyHZVf9o7e8=',
  ]
];

// prepare data to be encoded.
$data = [];

// iterate over the variables,
foreach ($vars as $var) {

  // prepare an array for the options
  $options = [
    "user" => "owncloud",
    "bucket" => $var['bucket'], // fill with current variable
    "password" => "",
    "region" => "regionOne",
    "service_name" => "swift",
    "tenant" => "owncloud",
    "timeout" => "30",
    "url" => "http:\/\/controller:5000\/v2.0",
    "password_encrypted" =>$var['pass'], // fill with current variable
  ];
  $userdata = [
    'id' => $var['id'], // fill with current variable
    'class' => '\\OC\\Files\\Storage\\Swift',
    'options' => $options, // append options
  ];
  $name = $var['user'];
  $path = $var['path'];
  $data['user'][$name][$path] = $userdata ; // append to $data array
}

echo json_encode($data, JSON_PRETTY_PRINT);

Outputs:

{
    "user": {
        "lam": {
            "\/lam\/files\/a": {
                "id": 9,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-15215156681752265498",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\\\/\\\/controller:5000\\\/v2.0",
                    "password_encrypted": "NHBuZHd4azhvZDB6b29oYSu5U7JLrDC3AdZGykzNpDU="
                }
            },
            "\/lam\/files\/test": {
                "id": 12,
                "class": "\\OC\\Files\\Storage\\Swift",
                "options": {
                    "user": "owncloud",
                    "bucket": "lam-152153961597103330",
                    "password": "",
                    "region": "regionOne",
                    "service_name": "swift",
                    "tenant": "owncloud",
                    "timeout": "30",
                    "url": "http:\\\/\\\/controller:5000\\\/v2.0",
                    "password_encrypted": "MjdzcDlrenptcG5udzI2MLSQvuGIczY\/SyHZVf9o7e8="
                }
            }
        }
    }
}

Upvotes: 1

Related Questions