0

Here is my Full Code. This script is triggered by a pub/sub in cloud functions, and parses out the message from the pub/sub by key, and value. for testing I have a sample string as the message in the code.

import base64
import os
import json
#import subprocess
#import shlex

    def hello_pubsub(event, context):
        """Triggered from a message on a Cloud Pub/Sub topic.
        Args:
             event (dict): Event payload.
             context (google.cloud.functions.Context): Metadata for the event.
        """
        pubsub_message ='{"name": instance1, "zone": us-west1-c, "project": projectname}'
        pubsub_json = json.loads(pubsub_message)
        for key,value in pubsub_json.items():
              print (key,value)



    hello_pubsub('event', 'context')

However, when I run this, I get this error

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 10 (char 9)

Using debugger in VSCode, the error gets trigger by the json.loads line.

I have tried using different versions of json.loads and Im not sure why I get this error, when I use json.dumps I get a sting/integers error. Any Ideas?

1
  • 1
    Your JSON is not properly quoted. Commented Oct 31, 2019 at 20:44

1 Answer 1

2

This is indeed not valid JSON. The strings need to be quoted:

pubsub_message ='{"name": "instance1", "zone": "us-west1-c", "project": "projectname"}'

I can't really understand what this is trying to test though.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to parse out name, zone, and project into 3 separate variables, so this is a test to see wether it can be parsed.
Also this answer worked. Thank you! sorry if it was a stupid question as I am new

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.