convert array of strings inside of string into a python list












0















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?










share|improve this question




















  • 2





    Possible duplicate of Parse JSON in Python

    – mhodges
    Nov 20 '18 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 '18 at 21:46






  • 2





    Works just fine for me. Are you certain that your data is in the format posted above?

    – mhodges
    Nov 20 '18 at 21:50











  • I'll double check to confirm

    – Brooklin Myers
    Nov 20 '18 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 '18 at 22:08


















0















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?










share|improve this question




















  • 2





    Possible duplicate of Parse JSON in Python

    – mhodges
    Nov 20 '18 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 '18 at 21:46






  • 2





    Works just fine for me. Are you certain that your data is in the format posted above?

    – mhodges
    Nov 20 '18 at 21:50











  • I'll double check to confirm

    – Brooklin Myers
    Nov 20 '18 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 '18 at 22:08
















0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:21







Brooklin Myers

















asked Nov 20 '18 at 21:41









Brooklin MyersBrooklin Myers

709




709








  • 2





    Possible duplicate of Parse JSON in Python

    – mhodges
    Nov 20 '18 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 '18 at 21:46






  • 2





    Works just fine for me. Are you certain that your data is in the format posted above?

    – mhodges
    Nov 20 '18 at 21:50











  • I'll double check to confirm

    – Brooklin Myers
    Nov 20 '18 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 '18 at 22:08
















  • 2





    Possible duplicate of Parse JSON in Python

    – mhodges
    Nov 20 '18 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 '18 at 21:46






  • 2





    Works just fine for me. Are you certain that your data is in the format posted above?

    – mhodges
    Nov 20 '18 at 21:50











  • I'll double check to confirm

    – Brooklin Myers
    Nov 20 '18 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 '18 at 22:08










2




2





Possible duplicate of Parse JSON in Python

– mhodges
Nov 20 '18 at 21:43





Possible duplicate of Parse JSON in Python

– mhodges
Nov 20 '18 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 '18 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 '18 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 '18 at 21:50





Works just fine for me. Are you certain that your data is in the format posted above?

– mhodges
Nov 20 '18 at 21:50













I'll double check to confirm

– Brooklin Myers
Nov 20 '18 at 22:06





I'll double check to confirm

– Brooklin Myers
Nov 20 '18 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 '18 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 '18 at 22:08














1 Answer
1






active

oldest

votes


















1














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']





share|improve this answer


























  • Isn't he doing the json.loads() solution?

    – Barmar
    Nov 20 '18 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 '18 at 22:17











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
});


}
});














draft saved

draft discarded


















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









1














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']





share|improve this answer


























  • Isn't he doing the json.loads() solution?

    – Barmar
    Nov 20 '18 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 '18 at 22:17
















1














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']





share|improve this answer


























  • Isn't he doing the json.loads() solution?

    – Barmar
    Nov 20 '18 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 '18 at 22:17














1












1








1







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']





share|improve this answer















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']






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 22:03

























answered Nov 20 '18 at 21:56









NathanNathan

1,306518




1,306518













  • Isn't he doing the json.loads() solution?

    – Barmar
    Nov 20 '18 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 '18 at 22:17



















  • Isn't he doing the json.loads() solution?

    – Barmar
    Nov 20 '18 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 '18 at 22:17

















Isn't he doing the json.loads() solution?

– Barmar
Nov 20 '18 at 22:12





Isn't he doing the json.loads() solution?

– Barmar
Nov 20 '18 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 '18 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 '18 at 22:17




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)