1

I'm creating a Phyton script to POST a request which contains JSON BODY, This is the JSON input I need to pass in the body of the request :

{
   "Name": "PServiceGroup125",
   "ServiceIDs": [1330,2387]
}

I'm able to do this(it works) :

   body = "{\"Name\": \"PServiceGroup125\",\"ServiceIDs\": [1330,2387]}"
   headers = [ NVPair('Content-Type', 'application/json'), NVPair('UAMToken', uamtok) ]
   response = self.httpPost(self.hostPort, self.URI, headers, body)

What I'm trying to achieve: ( to make it more "OO")

To create a function(say createBody ) to create a python object and then convert this python object into JSON and pass it in the request:

def createBody(self):
    self.serviceDataProvider.setName("PTestServiceGroup %s" % time.strftime("%d%m%H%M%S"))
    self.serviceDataProvider.setServiceIDs(self.serviceDataProvider.nextItem())

I have two questions here :

Here I set the "value" for the corresponding json name and service ID field, how should I set the name field(ServiceIDs)? ( "ServiceIDs": [1330,2387] )

Assuming that the "serviceDataProvider" object has been created successfully(with Name and ServiceID values ) .

how should i convert this object(serviceDataProvider) back to JSON and pass it in the request ?

I'm looking at the 'json' module , but not very clear on how to use it for this scenario

Anyones help and suggestion is appreciated !

# serviceDataProvider is object of class ServiceDataProvider 

class ServiceDataProvider(URLDataProvider):
    name =""
    sid=''
    sIDs=[]
    def setName(self,name):
        self.name=name

    def getName(self):
        return self.name

    def setServiceIDs(self,sid):
        sIDs=[]
        sIDs.append(sid)

    def getServiceIDs(self):
        return self.sIDs  

3 Answers 3

1

This is all a bit over-engineered, and not really very Pythonic: at a guess, your previous experience is in Java...

There's not really any need for a class here, and even less for those getter/setters. It would be better to build this up as a Python dictionary, which can then be converted directly into JSON.

body = {}
body['Name'] = "PTestServiceGroup %s" % time.strftime("%d%m%H%M%S")
body['ServiceIds'] = [service_id1, service_id2]  # wherever these come from

data = json.dumps(body)
Sign up to request clarification or add additional context in comments.

2 Comments

@ Daniel Roseman - Thanks , you got it , I'm was trying to do it the java way. This looks lot simpler, So I'm trying the understand the code snippet a little bit, the "body = {}" creates a List or string ? and the data should be passed in the POST request ?
No, {} is a dictionary (ie a HashMap) - and yes, data is the stringified JSON which you can pass in the POST body.
1

Try looking at json.dumps (converting a python dictionary structure to a json string) and json.loads (the reverse of dumps)

https://docs.python.org/2/library/json.html

Comments

1

Python is an adult-consenting language, you don't need setters/getters.

I would use a python dict or an OrderedDict if you want your json to be in the same order, since python dicts doesn't keep keys in the same order they were inserted.

Comments

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.