Robot Framework - TypeError: string indices must be integers When Parsing Json












0














I am trying to parse an array of json, But I am getting the following error



TypeError: string indices must be integers


Json:



{
'locationId': 'location1',
'name': 'Name',
'type': 'Ward',
'patientId': None,
'children': [{
'locationId': 'location2',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': [{
'locationId': 'location3',
'name': 'Name',
'type': 'HospitalGroup',
'patientId': None,
'children': None
}]
}, {
'locationId': 'location4',
'name': 'Name',
'type': 'Hospital',
'patientId': None,
'children': None
}, {
'locationId': 'location5',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location6',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location27',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}]
}


I am trying to the get all the locationId values and store the values one by one inside a list.



Here is what I am doing



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


I have also tried this



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['children'][0]['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


But I am getting the same error.



The test is failing on this line ${locationId}= set variable ${item['locationId']}



Any help would be apprectiated.










share|improve this question
























  • Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
    – A. Kootstra
    Nov 20 '18 at 13:31










  • @A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
    – Wojtek T
    Nov 20 '18 at 13:39










  • The format is Log Many ${Location_Json_List} Note the $
    – A. Kootstra
    Nov 20 '18 at 13:40










  • @A.Kootstra It is indeed the same structure as above json
    – Wojtek T
    Nov 20 '18 at 13:44










  • Apply the Log Many to the children and just loop through them.
    – A. Kootstra
    Nov 20 '18 at 14:01
















0














I am trying to parse an array of json, But I am getting the following error



TypeError: string indices must be integers


Json:



{
'locationId': 'location1',
'name': 'Name',
'type': 'Ward',
'patientId': None,
'children': [{
'locationId': 'location2',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': [{
'locationId': 'location3',
'name': 'Name',
'type': 'HospitalGroup',
'patientId': None,
'children': None
}]
}, {
'locationId': 'location4',
'name': 'Name',
'type': 'Hospital',
'patientId': None,
'children': None
}, {
'locationId': 'location5',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location6',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location27',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}]
}


I am trying to the get all the locationId values and store the values one by one inside a list.



Here is what I am doing



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


I have also tried this



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['children'][0]['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


But I am getting the same error.



The test is failing on this line ${locationId}= set variable ${item['locationId']}



Any help would be apprectiated.










share|improve this question
























  • Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
    – A. Kootstra
    Nov 20 '18 at 13:31










  • @A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
    – Wojtek T
    Nov 20 '18 at 13:39










  • The format is Log Many ${Location_Json_List} Note the $
    – A. Kootstra
    Nov 20 '18 at 13:40










  • @A.Kootstra It is indeed the same structure as above json
    – Wojtek T
    Nov 20 '18 at 13:44










  • Apply the Log Many to the children and just loop through them.
    – A. Kootstra
    Nov 20 '18 at 14:01














0












0








0







I am trying to parse an array of json, But I am getting the following error



TypeError: string indices must be integers


Json:



{
'locationId': 'location1',
'name': 'Name',
'type': 'Ward',
'patientId': None,
'children': [{
'locationId': 'location2',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': [{
'locationId': 'location3',
'name': 'Name',
'type': 'HospitalGroup',
'patientId': None,
'children': None
}]
}, {
'locationId': 'location4',
'name': 'Name',
'type': 'Hospital',
'patientId': None,
'children': None
}, {
'locationId': 'location5',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location6',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location27',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}]
}


I am trying to the get all the locationId values and store the values one by one inside a list.



Here is what I am doing



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


I have also tried this



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['children'][0]['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


But I am getting the same error.



The test is failing on this line ${locationId}= set variable ${item['locationId']}



Any help would be apprectiated.










share|improve this question















I am trying to parse an array of json, But I am getting the following error



TypeError: string indices must be integers


Json:



{
'locationId': 'location1',
'name': 'Name',
'type': 'Ward',
'patientId': None,
'children': [{
'locationId': 'location2',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': [{
'locationId': 'location3',
'name': 'Name',
'type': 'HospitalGroup',
'patientId': None,
'children': None
}]
}, {
'locationId': 'location4',
'name': 'Name',
'type': 'Hospital',
'patientId': None,
'children': None
}, {
'locationId': 'location5',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location6',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}, {
'locationId': 'location27',
'name': 'Name',
'type': 'Bed',
'patientId': None,
'children': None
}]
}


I am trying to the get all the locationId values and store the values one by one inside a list.



Here is what I am doing



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


I have also tried this



    @{locationIds}=  create list
:FOR ${item} IN @{Location_Json}
${locationId}= set variable ${item['children'][0]['locationId']}
log ${locationId}
append to list ${locationIds} '${locationId}'


But I am getting the same error.



The test is failing on this line ${locationId}= set variable ${item['locationId']}



Any help would be apprectiated.







json testing robotframework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:55

























asked Nov 20 '18 at 11:41









Wojtek T

1,246422




1,246422












  • Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
    – A. Kootstra
    Nov 20 '18 at 13:31










  • @A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
    – Wojtek T
    Nov 20 '18 at 13:39










  • The format is Log Many ${Location_Json_List} Note the $
    – A. Kootstra
    Nov 20 '18 at 13:40










  • @A.Kootstra It is indeed the same structure as above json
    – Wojtek T
    Nov 20 '18 at 13:44










  • Apply the Log Many to the children and just loop through them.
    – A. Kootstra
    Nov 20 '18 at 14:01


















  • Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
    – A. Kootstra
    Nov 20 '18 at 13:31










  • @A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
    – Wojtek T
    Nov 20 '18 at 13:39










  • The format is Log Many ${Location_Json_List} Note the $
    – A. Kootstra
    Nov 20 '18 at 13:40










  • @A.Kootstra It is indeed the same structure as above json
    – Wojtek T
    Nov 20 '18 at 13:44










  • Apply the Log Many to the children and just loop through them.
    – A. Kootstra
    Nov 20 '18 at 14:01
















Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
– A. Kootstra
Nov 20 '18 at 13:31




Can you check with a Log Many if your in-memory structure is indeed what you expect it is. It sounds to me that there is a list that you're trying to access using a string.
– A. Kootstra
Nov 20 '18 at 13:31












@A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
– Wojtek T
Nov 20 '18 at 13:39




@A.Kootstra I have changed the ${item['locationId']} to ${item["locationId"]} and now i am able to find only the first location value
– Wojtek T
Nov 20 '18 at 13:39












The format is Log Many ${Location_Json_List} Note the $
– A. Kootstra
Nov 20 '18 at 13:40




The format is Log Many ${Location_Json_List} Note the $
– A. Kootstra
Nov 20 '18 at 13:40












@A.Kootstra It is indeed the same structure as above json
– Wojtek T
Nov 20 '18 at 13:44




@A.Kootstra It is indeed the same structure as above json
– Wojtek T
Nov 20 '18 at 13:44












Apply the Log Many to the children and just loop through them.
– A. Kootstra
Nov 20 '18 at 14:01




Apply the Log Many to the children and just loop through them.
– A. Kootstra
Nov 20 '18 at 14:01












1 Answer
1






active

oldest

votes


















1














If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".



The json you are working with is a dictionary (not a list); thus, in this loop:



:FOR  ${item}  IN   @{Location_Json}


, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.



If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:



:FOR ${item} IN @{Location_Json['children']}



On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"



    ${locationId}=  set variable  ${item['locationId']}




Not related to your issue, put please do not do that:



append to list  ${locationIds}  '${locationId}'


Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.

Say, the value of ${locationId} is



location5


with those quotes surrounding it, it will end up as



'location5'


I don't think that's your goal; with these extra quotes, this will fail:



Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes





share|improve this answer





















  • Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
    – Wojtek T
    Nov 21 '18 at 9:40










  • I am able to get all the values one level down but i cannot get the location3 value
    – Wojtek T
    Nov 21 '18 at 9:46










  • Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
    – Todor Minakov
    Nov 21 '18 at 9:57






  • 1




    That's okay i think i will create a Custom Keyword for that, thanks
    – Wojtek T
    Nov 21 '18 at 9:58











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%2f53392257%2frobot-framework-typeerror-string-indices-must-be-integers-when-parsing-json%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














If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".



The json you are working with is a dictionary (not a list); thus, in this loop:



:FOR  ${item}  IN   @{Location_Json}


, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.



If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:



:FOR ${item} IN @{Location_Json['children']}



On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"



    ${locationId}=  set variable  ${item['locationId']}




Not related to your issue, put please do not do that:



append to list  ${locationIds}  '${locationId}'


Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.

Say, the value of ${locationId} is



location5


with those quotes surrounding it, it will end up as



'location5'


I don't think that's your goal; with these extra quotes, this will fail:



Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes





share|improve this answer





















  • Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
    – Wojtek T
    Nov 21 '18 at 9:40










  • I am able to get all the values one level down but i cannot get the location3 value
    – Wojtek T
    Nov 21 '18 at 9:46










  • Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
    – Todor Minakov
    Nov 21 '18 at 9:57






  • 1




    That's okay i think i will create a Custom Keyword for that, thanks
    – Wojtek T
    Nov 21 '18 at 9:58
















1














If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".



The json you are working with is a dictionary (not a list); thus, in this loop:



:FOR  ${item}  IN   @{Location_Json}


, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.



If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:



:FOR ${item} IN @{Location_Json['children']}



On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"



    ${locationId}=  set variable  ${item['locationId']}




Not related to your issue, put please do not do that:



append to list  ${locationIds}  '${locationId}'


Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.

Say, the value of ${locationId} is



location5


with those quotes surrounding it, it will end up as



'location5'


I don't think that's your goal; with these extra quotes, this will fail:



Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes





share|improve this answer





















  • Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
    – Wojtek T
    Nov 21 '18 at 9:40










  • I am able to get all the values one level down but i cannot get the location3 value
    – Wojtek T
    Nov 21 '18 at 9:46










  • Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
    – Todor Minakov
    Nov 21 '18 at 9:57






  • 1




    That's okay i think i will create a Custom Keyword for that, thanks
    – Wojtek T
    Nov 21 '18 at 9:58














1












1








1






If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".



The json you are working with is a dictionary (not a list); thus, in this loop:



:FOR  ${item}  IN   @{Location_Json}


, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.



If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:



:FOR ${item} IN @{Location_Json['children']}



On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"



    ${locationId}=  set variable  ${item['locationId']}




Not related to your issue, put please do not do that:



append to list  ${locationIds}  '${locationId}'


Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.

Say, the value of ${locationId} is



location5


with those quotes surrounding it, it will end up as



'location5'


I don't think that's your goal; with these extra quotes, this will fail:



Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes





share|improve this answer












If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".



The json you are working with is a dictionary (not a list); thus, in this loop:



:FOR  ${item}  IN   @{Location_Json}


, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.



If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:



:FOR ${item} IN @{Location_Json['children']}



On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"



    ${locationId}=  set variable  ${item['locationId']}




Not related to your issue, put please do not do that:



append to list  ${locationIds}  '${locationId}'


Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.

Say, the value of ${locationId} is



location5


with those quotes surrounding it, it will end up as



'location5'


I don't think that's your goal; with these extra quotes, this will fail:



Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 18:51









Todor Minakov

5,5862036




5,5862036












  • Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
    – Wojtek T
    Nov 21 '18 at 9:40










  • I am able to get all the values one level down but i cannot get the location3 value
    – Wojtek T
    Nov 21 '18 at 9:46










  • Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
    – Todor Minakov
    Nov 21 '18 at 9:57






  • 1




    That's okay i think i will create a Custom Keyword for that, thanks
    – Wojtek T
    Nov 21 '18 at 9:58


















  • Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
    – Wojtek T
    Nov 21 '18 at 9:40










  • I am able to get all the values one level down but i cannot get the location3 value
    – Wojtek T
    Nov 21 '18 at 9:46










  • Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
    – Todor Minakov
    Nov 21 '18 at 9:57






  • 1




    That's okay i think i will create a Custom Keyword for that, thanks
    – Wojtek T
    Nov 21 '18 at 9:58
















Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
– Wojtek T
Nov 21 '18 at 9:40




Thanks for the answer, that helped me solve my problem, also nice spot with the '${locationId}' thanks
– Wojtek T
Nov 21 '18 at 9:40












I am able to get all the values one level down but i cannot get the location3 value
– Wojtek T
Nov 21 '18 at 9:46




I am able to get all the values one level down but i cannot get the location3 value
– Wojtek T
Nov 21 '18 at 9:46












Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
– Todor Minakov
Nov 21 '18 at 9:57




Sounds like you need a general solution for "get all values of the key X in all sub-dictionaries that might be present in a list/dictionary", where X is "locationId" for you. Imagine the node that had location3 as value had something in the children key - you would then want to get the value location3.1 out from it too, I guess.That's a different problem from the one you've put in this question.
– Todor Minakov
Nov 21 '18 at 9:57




1




1




That's okay i think i will create a Custom Keyword for that, thanks
– Wojtek T
Nov 21 '18 at 9:58




That's okay i think i will create a Custom Keyword for that, thanks
– Wojtek T
Nov 21 '18 at 9:58


















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392257%2frobot-framework-typeerror-string-indices-must-be-integers-when-parsing-json%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

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]