TLDR: The GCF at https://github.com/krypted/tinyconverters/blob/main/GCF_YAML_to_PLIST.py will convert basic YAML to PLISTs. Here’s a Google Cloud Function that converts YAML to a property list. The function takes the YAML data as an event object, uses the yaml and plistlib modules to convert the YAML data to a property list, and returns the property list. To deploy this function, use the “Create Function” button in the Google Cloud Console. When prompted, select the Python 3.7 runtime and paste in the following script: Once the function is deployed, call it with a POST request to the function’s URL and include the YAML data. For example: Given the above input, the response body will…
-
-
Google Cloud Function to convert YAML to JSON
TLDR: Just posted this little Google Cloud Function to https://github.com/krypted/tinyconverters/blob/main/YAML_to_JSON.py. This Google Cloud Function converts YAML to JSON. The function takes YAML as input, uses the yaml and json modules, and returns the JSON data. The function below should be deployed as a Python 3.7 runtime: Once the function is deployed, call it by sending a POST request to the function’s URL with the YAML: The response body contains the JSON data as follows:
-
Lambda Function To Calc The Fields In A JSON Document
Below is a Lambda function to return the number of fields in a JSON document. This Lambda function expects the JSON document in a body field of the event. It loads the JSON document using the standard json.loads() and counts the number of fields by retrieving the keys of the loaded JSON object and calculating the length. The expected response would be a 200 status code if successful, with the number of fields as a string in the response body (which can easily be changed to an int, but made sense to leave as str so if it got some wacky data I’d see that). If the JSON document is…
-
Lambda Function To Calculate A Checksum
Here’s a Lambda function to calculate a checksum. The JSON document will be passed in the body field and the script loads the JSON document using json.loads(). The MD5 checksum of the JSON document is calculated by encoding the JSON string as bytes, hashing it using hashlib.md5(), and returning the hexadecimal representation of the hash using hexdigest(). The response returns a 200 status code when successful, with the MD5 checksum as a string in the response body. If the JSON document is invalid and cannot be loaded, a 400 status code is returned with an error message in the response body. In the event of a 400, use the https://github.com/krypted/tinyconverters/blob/main/LintJSON.py…
-
Google Cloud Function To Validate Email Addresses
The following is a simple Google Cloud Function that will validate that text entered into a field (passed to the function in the request body when called) is a valid format of an email address. The email address might not be real, but at least we aren’t accepting a string that’s incorrectly formatted: This function will validate the email address that is passed in the request body. If the email address is valid, the function will return a success response (200). If the email address is not valid, the function will return an error response (400).
-
Google Cloud Function To Add JSON To A Cloud SQL Database
Here’s a Google Cloud Function that takes some arbitrary json (in json_data) and posts it to a new record in a Cloud SQL database. No error handling or deduplication/matching, just a straight post: https://github.com/krypted/tinyconverters/blob/548892cc377e1063770ab4a8cd53dc6573bae950/json_to_cloud_SQL.py Before using, it needs information to connect to the database, so customize the INSTANCE_NAME, DATABASE_NAME, and TABLE_NAME for the INSERT. For more on Cloud SQL, see https://cloud.google.com/sql/docs.
-
Google Cloud Function To Create A Google Doc
Here’s a Google Cloud Function that creates a Google Doc based on a title and body from a json document: https://github.com/krypted/tinyconverters/blob/main/json_to_new_google_doc.py To use it, you’ll need to enable the Google Docs API from the Google Cloud Project and create a service account to get a JSON key file that’s saved in the same folder the cloud function is uploaded to. Once the Cloud Function is deployed, grab the URL and use it as follows:
-
Google Cloud Function To Add JSON To A Google Sheet
Here’s a Google Cloud Function that uses the Google Sheets API to add arbitrary json to a Google Sheet: https://github.com/krypted/tinyconverters/blob/main/json_to_GoogleSheet.py To use the function, create a Google Cloud project and enable the Google Sheets API. Then create a service account and download the JSON key file for that account and place it in the same directory as the script. Once the Cloud Function is deployed, a request similar to the following will add the nested JSON data to the spreadsheet with the ID for that sheet swapped in with spreadsheetId in the below `curl`: This allows us to pipeline information in a variety of ways, like a web hook that…
-
Google Cloud Function To Check Status Of A Port
Sometimes you just need a little microservice to check that a port is up before you try and send some data to it. This does just that… https://github.com/krypted/NestMonitor/blob/main/socket%20check.py
-
Google Cloud Function To Convert Property Lists To JSON Documents
I’ve written a lot of little scripts to convert files or types from one format to another, over the years. It’s easier to import something that can do the work for you and just use a Lambda or a Google Cloud Function that you call from other tools. This is the microservice way. So here’s a little python Google Cloud Function that converts a property list to a JSON document: To use the function, first create a Google Cloud Function that uses the property_list_to_json() function from the Google Cloud Console or the gcloud CLI. Once you have the URL, simply call it with a POST to the function’s URL. The…