convert array of strings inside of string into a python list
I've sent some form-data from my React client to my Django + DRF api.
inside of the form-data I have an attribute date_strings
which is an array of date-time strings. i.e. ["date1", "date2", "date3"]
in order to send it to my Django api I converted the array of strings into a string using JSON.stringify
const myForm = new FormData();
myForm.set("date_strings", JSON.stringify(dateStrings));
in the create method of my serializer, I'd like to convert this data into a list.
def create(self, validated_data):
stringified_array = validated_data.pop('date_strings')
// stringified_array: '["date1", "date2", "date3"]'
how can I convert an array of strings inside of a string into a python list?
python json django parsing
|
show 2 more comments
I've sent some form-data from my React client to my Django + DRF api.
inside of the form-data I have an attribute date_strings
which is an array of date-time strings. i.e. ["date1", "date2", "date3"]
in order to send it to my Django api I converted the array of strings into a string using JSON.stringify
const myForm = new FormData();
myForm.set("date_strings", JSON.stringify(dateStrings));
in the create method of my serializer, I'd like to convert this data into a list.
def create(self, validated_data):
stringified_array = validated_data.pop('date_strings')
// stringified_array: '["date1", "date2", "date3"]'
how can I convert an array of strings inside of a string into a python list?
python json django parsing
2
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
2
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I think the problem is probably with your input,[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...
– Nathan
Nov 20 at 22:08
|
show 2 more comments
I've sent some form-data from my React client to my Django + DRF api.
inside of the form-data I have an attribute date_strings
which is an array of date-time strings. i.e. ["date1", "date2", "date3"]
in order to send it to my Django api I converted the array of strings into a string using JSON.stringify
const myForm = new FormData();
myForm.set("date_strings", JSON.stringify(dateStrings));
in the create method of my serializer, I'd like to convert this data into a list.
def create(self, validated_data):
stringified_array = validated_data.pop('date_strings')
// stringified_array: '["date1", "date2", "date3"]'
how can I convert an array of strings inside of a string into a python list?
python json django parsing
I've sent some form-data from my React client to my Django + DRF api.
inside of the form-data I have an attribute date_strings
which is an array of date-time strings. i.e. ["date1", "date2", "date3"]
in order to send it to my Django api I converted the array of strings into a string using JSON.stringify
const myForm = new FormData();
myForm.set("date_strings", JSON.stringify(dateStrings));
in the create method of my serializer, I'd like to convert this data into a list.
def create(self, validated_data):
stringified_array = validated_data.pop('date_strings')
// stringified_array: '["date1", "date2", "date3"]'
how can I convert an array of strings inside of a string into a python list?
python json django parsing
python json django parsing
edited Nov 20 at 22:21
asked Nov 20 at 21:41
Brooklin Myers
558
558
2
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
2
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I think the problem is probably with your input,[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...
– Nathan
Nov 20 at 22:08
|
show 2 more comments
2
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
2
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I think the problem is probably with your input,[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...
– Nathan
Nov 20 at 22:08
2
2
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
2
2
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I think the problem is probably with your input,
[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...– Nathan
Nov 20 at 22:08
I think the problem is probably with your input,
[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...– Nathan
Nov 20 at 22:08
|
show 2 more comments
1 Answer
1
active
oldest
votes
You can easily turn strings into lists using .split
. However, you have to remove the outermost characters ["
and "]
because otherwise these will also be added to your list of strings.
stringified = '["date1", "date2", "date3"]'
unstringified = stringified[2:-2].split('", "')
returns
['date1', 'date2', 'date3']
A much better solution as mentioned by @mhodges
import json
stringified = '["date1", "date2", "date3"]'
unstringified = json.loads(stringified)
Which also outputs:
['date1', 'date2', 'date3']
Isn't he doing thejson.loads()
solution?
– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
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%2f53402003%2fconvert-array-of-strings-inside-of-string-into-a-python-list%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 easily turn strings into lists using .split
. However, you have to remove the outermost characters ["
and "]
because otherwise these will also be added to your list of strings.
stringified = '["date1", "date2", "date3"]'
unstringified = stringified[2:-2].split('", "')
returns
['date1', 'date2', 'date3']
A much better solution as mentioned by @mhodges
import json
stringified = '["date1", "date2", "date3"]'
unstringified = json.loads(stringified)
Which also outputs:
['date1', 'date2', 'date3']
Isn't he doing thejson.loads()
solution?
– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
add a comment |
You can easily turn strings into lists using .split
. However, you have to remove the outermost characters ["
and "]
because otherwise these will also be added to your list of strings.
stringified = '["date1", "date2", "date3"]'
unstringified = stringified[2:-2].split('", "')
returns
['date1', 'date2', 'date3']
A much better solution as mentioned by @mhodges
import json
stringified = '["date1", "date2", "date3"]'
unstringified = json.loads(stringified)
Which also outputs:
['date1', 'date2', 'date3']
Isn't he doing thejson.loads()
solution?
– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
add a comment |
You can easily turn strings into lists using .split
. However, you have to remove the outermost characters ["
and "]
because otherwise these will also be added to your list of strings.
stringified = '["date1", "date2", "date3"]'
unstringified = stringified[2:-2].split('", "')
returns
['date1', 'date2', 'date3']
A much better solution as mentioned by @mhodges
import json
stringified = '["date1", "date2", "date3"]'
unstringified = json.loads(stringified)
Which also outputs:
['date1', 'date2', 'date3']
You can easily turn strings into lists using .split
. However, you have to remove the outermost characters ["
and "]
because otherwise these will also be added to your list of strings.
stringified = '["date1", "date2", "date3"]'
unstringified = stringified[2:-2].split('", "')
returns
['date1', 'date2', 'date3']
A much better solution as mentioned by @mhodges
import json
stringified = '["date1", "date2", "date3"]'
unstringified = json.loads(stringified)
Which also outputs:
['date1', 'date2', 'date3']
edited Nov 20 at 22:03
answered Nov 20 at 21:56
Nathan
1,197518
1,197518
Isn't he doing thejson.loads()
solution?
– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
add a comment |
Isn't he doing thejson.loads()
solution?
– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
Isn't he doing the
json.loads()
solution?– Barmar
Nov 20 at 22:12
Isn't he doing the
json.loads()
solution?– Barmar
Nov 20 at 22:12
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message
["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
This is the correct answer! I made a mistake, json.loads() works correctly. I tried using json.loads before, but I had a log below it so kept getting the message
["TypeError: must be str, not typen"]
– Brooklin Myers
Nov 20 at 22:17
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53402003%2fconvert-array-of-strings-inside-of-string-into-a-python-list%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
2
Possible duplicate of Parse JSON in Python
– mhodges
Nov 20 at 21:43
I've tried using json.loads but it doesn't work. I think this is because this is an array and not formatted json
– Brooklin Myers
Nov 20 at 21:46
2
Works just fine for me. Are you certain that your data is in the format posted above?
– mhodges
Nov 20 at 21:50
I'll double check to confirm
– Brooklin Myers
Nov 20 at 22:06
I think the problem is probably with your input,
[", "name": "server.posts.serializers", "levelname": "INFO", "request_id": "none"}
doesn't seem right to me...– Nathan
Nov 20 at 22:08