0
import requests
import json

url='http://test.com/job/MY_JOB_NAME/build'
params={'name':'BRANCH', 'value':'master', 'name':'GITURL', 'value':'https://github.test.com/test/test.git'}
payload = json.dumps(params)
print payload
resp = requests.post(url=url, data=payload)

For some reason the request doesn't get executed successfully, so I print the payload to see what paramaters are being passed as json and I get this:

{"name": "GITURL", "value": "https://github.scm.corp.ebay.com/RPS/RPS.git"}

Why is my payload missing the first 2 json key-value pairs ?

1
  • Dictionary stores only unique keys. Commented Feb 12, 2014 at 8:27

2 Answers 2

2

It's not the problem of the json.dumps.

In the dict literal, same keys are present. Keys should be unique.

>>> {'a': 'b', 'a': 'c'}
{'a': 'c'}

Use different keys, or make values as list:

>>> {'a1': 'b', 'a2': 'c'}
{'a1': 'b', 'a2': 'c'}
>>> {'a': ['b' ,'c']}
{'a': ['b', 'c']}

or use a list of dictionaries:

>>> [{'a': 'b'}, {'a': 'c'}]
[{'a': 'b'}, {'a': 'c'}]
Sign up to request clarification or add additional context in comments.

7 Comments

What if i want the same keys to be repeated twice ? Is there any other alternative ?
@PiHorse, You can't (with a single dictionary).
@PiHorse, I added an alternative that use list with dictionaries items.
I tried that, but then it changes the json format which I need in my parameters for making the request.
@PiHorse, JSON dictionary (hash table, object) also requires keys to be unique.
|
1

You have two identical keys, name and value.

Change the names.

params={'branch':'BRANCH', 'tree':'master', 'name':'GITURL', 'value':'https://github.test.com/test/test.git'}

JSON is just like the dictionary in Python. They are matched on a Key = Value basis, and each key is a uiniqieue identifier.

x = {}
x['elephant'] = 1

And if another elephant comes along,

x['elephant'] += 1

But you can't do:

x['elephant'] = 'has a trunk'

Because, then you replace the number of elephants with how they look.

1 Comment

What if i want the same keys to be repeated twice ? Is there any other alternative ?

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.