Remove keys with `None` value from body in requests
Using Python's requests
package, I have a body for a POST that has a bunch of None
values, ex (dictionary, not JSON):
{
'name': 'John',
'surname': None,
'somelist': [
{
'a': 1,
'b': None
},
{
'a': None,
'b': 2
}
],
'otherdict': {
'c': False,
'd': None
}
}
and I would like the actual body that gets sent out has all the entries with values None
removed, as opposed to converted to JSON null
:
{
'name': 'John',
'somelist': [
{
'a': 1
},
{
'b': 2
}
],
'otherdict': {
'c': False
}
}
Does the requests
package have an option that does this or do I need to do it on my end?
python python-requests
add a comment |
Using Python's requests
package, I have a body for a POST that has a bunch of None
values, ex (dictionary, not JSON):
{
'name': 'John',
'surname': None,
'somelist': [
{
'a': 1,
'b': None
},
{
'a': None,
'b': 2
}
],
'otherdict': {
'c': False,
'd': None
}
}
and I would like the actual body that gets sent out has all the entries with values None
removed, as opposed to converted to JSON null
:
{
'name': 'John',
'somelist': [
{
'a': 1
},
{
'b': 2
}
],
'otherdict': {
'c': False
}
}
Does the requests
package have an option that does this or do I need to do it on my end?
python python-requests
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value'null'
instead of JSON valuenull
and these are all optional values, so it's easier for me to just not include them than to convertNone
to string'null'
.
– Joao Coelho
Nov 15 '18 at 23:15
1
No such param inrequests
, also no such function in it. You need to convert None to 'null' by yourself
– kcorlidy
Nov 16 '18 at 11:03
add a comment |
Using Python's requests
package, I have a body for a POST that has a bunch of None
values, ex (dictionary, not JSON):
{
'name': 'John',
'surname': None,
'somelist': [
{
'a': 1,
'b': None
},
{
'a': None,
'b': 2
}
],
'otherdict': {
'c': False,
'd': None
}
}
and I would like the actual body that gets sent out has all the entries with values None
removed, as opposed to converted to JSON null
:
{
'name': 'John',
'somelist': [
{
'a': 1
},
{
'b': 2
}
],
'otherdict': {
'c': False
}
}
Does the requests
package have an option that does this or do I need to do it on my end?
python python-requests
Using Python's requests
package, I have a body for a POST that has a bunch of None
values, ex (dictionary, not JSON):
{
'name': 'John',
'surname': None,
'somelist': [
{
'a': 1,
'b': None
},
{
'a': None,
'b': 2
}
],
'otherdict': {
'c': False,
'd': None
}
}
and I would like the actual body that gets sent out has all the entries with values None
removed, as opposed to converted to JSON null
:
{
'name': 'John',
'somelist': [
{
'a': 1
},
{
'b': 2
}
],
'otherdict': {
'c': False
}
}
Does the requests
package have an option that does this or do I need to do it on my end?
python python-requests
python python-requests
asked Nov 15 '18 at 23:13
Joao CoelhoJoao Coelho
98521423
98521423
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value'null'
instead of JSON valuenull
and these are all optional values, so it's easier for me to just not include them than to convertNone
to string'null'
.
– Joao Coelho
Nov 15 '18 at 23:15
1
No such param inrequests
, also no such function in it. You need to convert None to 'null' by yourself
– kcorlidy
Nov 16 '18 at 11:03
add a comment |
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value'null'
instead of JSON valuenull
and these are all optional values, so it's easier for me to just not include them than to convertNone
to string'null'
.
– Joao Coelho
Nov 15 '18 at 23:15
1
No such param inrequests
, also no such function in it. You need to convert None to 'null' by yourself
– kcorlidy
Nov 16 '18 at 11:03
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value
'null'
instead of JSON value null
and these are all optional values, so it's easier for me to just not include them than to convert None
to string 'null'
.– Joao Coelho
Nov 15 '18 at 23:15
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value
'null'
instead of JSON value null
and these are all optional values, so it's easier for me to just not include them than to convert None
to string 'null'
.– Joao Coelho
Nov 15 '18 at 23:15
1
1
No such param in
requests
, also no such function in it. You need to convert None to 'null' by yourself– kcorlidy
Nov 16 '18 at 11:03
No such param in
requests
, also no such function in it. You need to convert None to 'null' by yourself– kcorlidy
Nov 16 '18 at 11:03
add a comment |
1 Answer
1
active
oldest
votes
You can do this with recursion (remove keys with None
values or recursively call the clean
method if the value is not None
):
def clean(d):
if type(d) == list:
return [clean(e) for e in d]
elif type(d) == dict:
for k, v in list(d.items()):
if v is None:
del d[k]
else:
d[k] = clean(v)
return d
print(clean(data))
Output
{'somelist': [{'a': 1}, {'b': 2}], 'otherdict': {'c': False}, 'name': 'John'}
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329191%2fremove-keys-with-none-value-from-body-in-requests%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this with recursion (remove keys with None
values or recursively call the clean
method if the value is not None
):
def clean(d):
if type(d) == list:
return [clean(e) for e in d]
elif type(d) == dict:
for k, v in list(d.items()):
if v is None:
del d[k]
else:
d[k] = clean(v)
return d
print(clean(data))
Output
{'somelist': [{'a': 1}, {'b': 2}], 'otherdict': {'c': False}, 'name': 'John'}
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
add a comment |
You can do this with recursion (remove keys with None
values or recursively call the clean
method if the value is not None
):
def clean(d):
if type(d) == list:
return [clean(e) for e in d]
elif type(d) == dict:
for k, v in list(d.items()):
if v is None:
del d[k]
else:
d[k] = clean(v)
return d
print(clean(data))
Output
{'somelist': [{'a': 1}, {'b': 2}], 'otherdict': {'c': False}, 'name': 'John'}
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
add a comment |
You can do this with recursion (remove keys with None
values or recursively call the clean
method if the value is not None
):
def clean(d):
if type(d) == list:
return [clean(e) for e in d]
elif type(d) == dict:
for k, v in list(d.items()):
if v is None:
del d[k]
else:
d[k] = clean(v)
return d
print(clean(data))
Output
{'somelist': [{'a': 1}, {'b': 2}], 'otherdict': {'c': False}, 'name': 'John'}
You can do this with recursion (remove keys with None
values or recursively call the clean
method if the value is not None
):
def clean(d):
if type(d) == list:
return [clean(e) for e in d]
elif type(d) == dict:
for k, v in list(d.items()):
if v is None:
del d[k]
else:
d[k] = clean(v)
return d
print(clean(data))
Output
{'somelist': [{'a': 1}, {'b': 2}], 'otherdict': {'c': False}, 'name': 'John'}
answered Nov 15 '18 at 23:22
sliderslider
8,49811331
8,49811331
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
add a comment |
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
1
1
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
Yeah, I figured as much. Just wated to check if the requests package already has an option to do it so I don' t have to. If not, I'll prolly use your code snippet.
– Joao Coelho
Nov 15 '18 at 23:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329191%2fremove-keys-with-none-value-from-body-in-requests%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note: The reason I'm asking this is b/c the web service doesn't conform to the REST API and needs string value
'null'
instead of JSON valuenull
and these are all optional values, so it's easier for me to just not include them than to convertNone
to string'null'
.– Joao Coelho
Nov 15 '18 at 23:15
1
No such param in
requests
, also no such function in it. You need to convert None to 'null' by yourself– kcorlidy
Nov 16 '18 at 11:03