Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

way to create / import data using existing JSON as payload #147

Open
nkuehn opened this issue Aug 26, 2015 · 3 comments
Open

way to create / import data using existing JSON as payload #147

nkuehn opened this issue Aug 26, 2015 · 3 comments

Comments

@nkuehn
Copy link
Contributor

nkuehn commented Aug 26, 2015

please provide a (or document an existing) way to create / import data using JSON files.

example where it came up: a 3rd party tool generates product drafts as JSON which should be used in ProductCreateRequests instead of building the Product Drafts on the PHP API.

what happened is that the user did not use the PHP SDK for that but did it all manually (and ran into OAuth issues). It's propbably just documentation, but I think it would be helpful to provide explicit named constructors "ofDraftJson" or similar to either the Create Requests or the Draft Model Objects.

It's performance sensitive (batch imports), so there should not be a full object mapping roundtrip involved (validation is done platform-side anyways). JSON could be provided either as a String or as a PHP associative array.

@eventhorizonpl
Copy link

+1

Another use case would be replication of data from one database to another. ofObject would be a killer feature here.

@barrycarton
Copy link

+1

@nkuehn
Copy link
Contributor Author

nkuehn commented Feb 2, 2016

Hi, as there seems to be demand: Every Model Object in the SDK (in this case you're probably looking for the *Draft ones) can be created via its fromArray() function that takes the associative array created by PHP's own json_decode().
The same applies in the opposite direction: all the model objects are done to be cleanly serialized to JSON via json_encode($myCommercetoolsModel) .

So practically spoken the following (UNTESTED!) code is the pattern how you can do a bulk import using PHP:

<?php

use Commercetools\Core\Model\Product\ProductDraft;
use Commercetools\Core\Request\Products\ProductCreateRequest;
use Commercetools\Core\Client;
use Commercetools\Core\Config;

$client = Client::ofConfig(Config::fromArray(["clientId" => ""/* to do complete config*/]));

// for every batch of e.g. 100 products do:
    // for each product:
    $productDraftAsArray = json_decode(file_get_contents("./myFile.json"));
    $productDraft = ProductDraft::fromArray($productDraft);
    $addProductRequest = ProductCreateRequest::ofDraft($productDraft);
    $client->addBatchRequest($addProductRequest);
    //
$client->executeBatch();  // SDK will do the requests with a parallelity of (sensible default) 25
echo("added a batch of products");
//
// do next batch

That being said, IF you're importing Products, Prices (JSON or CSV) or other stuff (just CSV) you will still be faster and more robust using the Command-Line import / export tooling. Especially if you're updating / synchronizing and not just initially bulk importing. Generating the necessary update actions by "diffing" the current and new JSON is a real pain these tools do for you.
https://github.com/sphereio/sphere-product-import // http://sphereio.github.io/nodejs/

(@jayS-de FYI in case you have something better or tested available)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants