Rajaneesh
Rajaneesh

Reputation: 1

How to insert data in to datatstore

How to insert data in to datastore? The data could be like the one below:

{
  'food': [{
    "item_name": item,
    'price': price
  }, {
    "item_name": item,
    'price': price
  }],
  'beverages': [{
    ''
    'beverage_name': beverage,
    'beverage_price': b_price
  }, {
    ''
    'beverage_name': beverage,
    'beverage_price': b_price
  }]
}

Upvotes: 0

Views: 818

Answers (1)

Andrei Cusnir
Andrei Cusnir

Reputation: 2805

The data that you are trying to add to the Google Cloud Datastore is a JSON string. The way you have it in your question is wrong structured. The proper JSON example would be:

{
  "food": [
    { "food_name":"NAME1", "food_price":"PRICE1" },
    { "food_name":"NAME2", "food_price":"PRICE2" },
    { "food_name":"NAME3", "food_price":"PRICE3" }
  ],
  "beverages":[
    { "beverage_name":"NAME1", "beverage_price":"PRICE1" },
    { "beverage_name":"NAME2", "beverage_price":"PRICE2" }
  ]
}

To add the data from the JSON string to the Datastore you have to:

  1. Load the JSON string as JSON object to be able to go through its fields
  2. Create a client to access Google Datastore
  3. Set the key food for the Kind value in Datastore
  4. Use the entity to add the data to the Datastore
  5. Set the key beverages for the Kind value in Datastore
  6. Use again entity to add the data to the Datastore

For further information, you can refer to Google Cloud Data Store Entities, Properties, and Keys documentation.

I have done a little bit coding myself and here is my code example in GitHub for Python. You can take the idea of how it works and test it. It will create two different Kind values in Datastore and add the food data in foods and beverage data to beverages.

Upvotes: 1

Related Questions