

One way or another, you’re up to your neck in JSON, and you’ve got to Python your way out. Perhaps you’re gathering information through an API or storing your data in a document database. Chances are you’re here because you need to transport some data from here to there. Since its inception, JSON has quickly become the de facto standard for information exchange. Watch it together with the written tutorial to deepen your understanding: Working With JSON Data in Python
Json query tutorial update#
We'll assume the player has already reached every checkpoint, now we just need to update the "complete" attribute in the document: ".LogEventRequest",Īgain, we can now query the level 1 data for the user at any point and get the full details: ".Watch Now This tutorial has a related video course created by the Real Python team. Once this request has been passed in, we can query the document to get the progress in the future if required: ".LogEventRequest",

We want to update the document to put the checkpoint reached into the document: ".LogEventRequest", We can see now that the user has no data for level 1. We'll first check if the user has any data for level 1: ".LogEventRequest", tScriptData( "data", partialResult) Įxecution of the Configuration Register a User to Use for the Tests ".RegistrationRequest", Run the aggregation var partialResult = progressCollection.aggregate(matchObject, projectObject) We need to create JSON object using a string as a key //We do this using the notation var updateObject = Get the value that was passed in for VAL var inputValue = Spark. Get the value that was passed in for PATH var inputPath = Spark. This script uses a little bit of Mongo magic to create the document if it doesn't already exist, and sets the value you want at the path you ask for: var progressCollection = ntimeCollection( "player_progress")
Json query tutorial code#
We now need to bind a Cloud Code script to this Event to access the data and make the document updates. We'll add a new Event in the portal that looks like this:Ĭreating the Script to Process the Update Event

If this sounds a little complex, bear with us and work through the example - it should all become clear by the time you've work through the example!

This Event will contain the path of the document we want to update and the value we want to set at that path. We're creating a generic Event for updating a document. Configuring the Game Creating the Event for Updating the Document.
Json query tutorial how to#
More Information! For further details and recommendations on how to use MongoDB collections to store and manage your game data, see the Managing Data Persistence tutorial. In this state, the document we store against the user will look like this: We'll imagine we have a player who has already played level 1 and completed it, and has started level 2 but has not yet completed it.When the player starts a level you want to return the current state from the server as a full document, but without the details of the other levels.We might want to query for a particular section of the document without returning the full document.Ī concrete example of this sort of case might be where you are storing custom data about every level the player has ever played:.We'll assume the complete document is the full state of the game, but we don't want to pass the full document around every time we want to update it.For the alternative tutorial for new games using the Game Data Service, see Submitting JSON Document Queries Using Game Data Service. Important! If you're working on a new game that was created after the Game Data Service was launched in January 2018, you won't be able to create new Mongo Runtime collections and the Cloud Code used in the tutorial will not work. There are times where you want to maintain a complex JSON document in a Mongo collection that you want to use for storing state. How to do Partial Queries and Updates to a Complex JSON Document
