JSON

The Cloud CMS Bulk Import tool makes it easy to import JSON content into a Cloud CMS content repository. JSON is a common format for content that has been exported from an existing database or legacy CMS system.

Suppose that we have a JSON file containing an array of player information from the 2014 World Cup. This file is located at data/2014-world-cup-squads.json and looks more or less like this:

[{
    "Team": "Portugal",
    "Number": "7",
    "Position": "FW",
    "FullName": "Cristiano Ronaldo",
    "Club": "Real Madrid C.F.",
    "ClubCountry": "ESP",
    "DateOfBirth": "1985-2-5",
    "IsCaptain": true
}, {
    "Team": "Argentina",
    "Number": "10",
    "Position": "FW",
    "FullName": "Lionel Messi",
    "Club": "FC Barcelona",
    "ClubCountry": "ESP",
    "DateOfBirth": "1987-6-24",
    "IsCaptain": true
}, {
    "Team": "Brazil",
    "Number": "10",
    "Position": "FW",
    "FullName": "Neymar",
    "Club": "FC Barcelona",
    "ClubCountry": "ESP",
    "DateOfBirth": "1992-2-5",
    "IsCaptain": false
}]

Of course, the file may have many more entries in it. But the three shown above are representative. For each entry, we have information about the player and what team and country they play for.

We can import these images into Cloud CMS by using the cloudcms-packager module like this:

// create a packager
var PackagerFactory = require("cloudcms-packager");
PackagerFactory.create(function(err, packager) {

    // load in content from JSON
    var array = require("./data/2014-world-cup-squads.json");
    for (var i = 0; i < array.length; i++)
    {
        var obj = JSON.parse(JSON.stringify(array[i]));

        packager.addNode(obj);
    }

    // commit
    packager.package(function(err, archiveInfo) {
        console.log("Wrote archive: " + archiveInfo.filename);
    });

});

This will create content instances for all of the objects in the array. We will have a content instance for Ronaldo, Messi and Neymar. As well as everyone else in the array.

By default, these players will all have the content type n:node which is the generic content type given when nothing else is provided. What if we want to provide a custom content type? We can define one by simply adding a separate file to disk called types/my_athlete/node.json. It defines a content type using JSON Schema.

It looks like this:

{
    "title": "Athlete",
    "type": "object",
    "properties": {
        "title": {
            "title": "Title",
            "type": "string"
        },
        "Team": {
            "title": "Team",
            "type": "string"
        },
        "Number": {
            "title": "Number",
            "type": "string"
        },
        "Position": {
            "title": "Position",
            "type": "string"
        },
        "FullName": {
            "title": "Full Name",
            "type": "string"
        },
        "Club": {
            "title": "Club",
            "type": "string"
        },
        "ClubCountry": {
            "title": "Club Country",
            "type": "string"
        },
        "DateOfBirth": {
            "title": "Date of Birth",
            "type": "string"
        },
        "IsCaptain": {
            "title": "Is Captain",
            "type": "boolean"
        }
    },
    "_qname": "my:athlete",
    "_type": "d:type"
}

We can then adjust our code to import the content type as well as the content instances. For each content instance, we set it's _type to my:athlete, like this:

// create a packager
var PackagerFactory = require("cloudcms-packager");
PackagerFactory.create(function(err, packager) {

    // package up the "my:athlete" content type definition
    packager.addFromDisk("./types/my_athlete/node.json");

    // load in content from JSON
    var array = require("./data/2014-world-cup-squads.json");
    for (var i = 0; i < array.length; i++)
    {
        var obj = JSON.parse(JSON.stringify(array[i]));
        obj._type = "my:athlete";

        packager.addNode(obj);
    }

    // commit
    packager.package(function(err, archiveInfo) {
        console.log("Wrote archive: " + archiveInfo.filename);
    });

});

For a full example of this code in action, please see:

https://github.com/gitana/cloudcms-packager/tree/master/examples/json/world-cup-2014-squads/example2