Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question

How can I convert json to SpaceDocument

Presently, I am storing data in NOSQL DB in json format. Now, I want to store same json in GS. How can I convert same json to SpaceDocument. If I am correct, SpaceDocument is Key-value data structure. I would not like refactor my POJO-JSON code base.

asked 2017-02-04 06:56:28 -0500

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted


Please see the code snippet below on how to declare and transform a JSON document to a space document. The strategy in this example is simply to convert JSON to MAP and pass it to the Space Document Constructor.


Although you define an Id, it is not a key-value store because you can access data without knowing the object's key, but by using rich SQL semantics with field level access. (location='NY' AND stockQuantity>50).

There is also nested fields as well so that if you object map/json model has multiple levels of depth. you can query on those properties directly without iterating the tree. (address.city='NYC' AND stockQuantity>50)


We also provide Semi Structured Data modeling as well as Structured and unstructured data types. You could essentially define some static properties but have dynamic portion for data variability. With this model you could parse is using Pojo/JSon mappers.


     * We Need to Register a type by specifying a name, id, and routing field.
     * Routing is used to partition data across the grid, similar to a shard key 
     * This only needs to be done once 
    final String PRODUCT_TYPE_NAME = "com.mycompany.organization.application.Product";

    SpaceTypeDescriptor typeDescriptor = new SpaceTypeDescriptorBuilder(PRODUCT_TYPE_NAME)
                .addPropertyIndex("processed", SpaceIndexType.BASIC)


     * Example JSON payload containing the properties 
     * of the new Type. In this example we use jaxson object mapper.
     * You can use any parser you would like
    String jsonPayload = "{\"id\":1, \"location\":\"usa\", \"processed\":false}";

    HashMap<String,Object> jsonProperties =
            new ObjectMapper().readValue(jsonPayload, HashMap.class);

     * Convert to a space document simply pass the hashmap to the 
     * in to the SpaceDocument Constructor along with the document type name
     * from above
    SpaceDocument dataAsDocument = new SpaceDocument(PRODUCT_TYPE_NAME, jsonProperties);

     * Insert to the grid

     * To confirm the result. Read the document from the grid
    SpaceDocument dataAsDocumentFromGrid = gigaSpace.read(new SQLQuery<SpaceDocument>(PRODUCT_TYPE_NAME, "id = ?", 1));

     * Map the Object back to JSON
    String jsonFromGrid = new ObjectMapper().writeValueAsString(dataAsDocumentFromGrid);


answered 2017-05-05 11:32:17 -0500

Michael Raney gravatar image

updated 2017-05-05 11:33:20 -0500

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-02-04 06:56:28 -0500

Seen: 21 times

Last updated: May 05