Python Forum

Full Version: Question about working with dictionaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello forum,

I am having the current hurdle working with dictionaries.
Concrete: I am trying to add variable amounts of components to a JIRA issue.

Example:
If "Linux" is selected, I want to have the distribution added as second component as well.
Result in components: "Linux", "Mint"

JIRA required to have the following structure
[{ "name" : "component" }]
or for multiple components
[{ "name" : "component" }, { "name" : "component" }]
What works is something like:
[{ "name" : VARIABLE }]
or
[{ "name" : VARIABLE }, { "name" : SECOND_VARIABLE }]
What is now my problem:
I need to have this structure depending on what is selected before as having empty variables is not valid for the cases having only one component.

If e.g. "Windows" is selected I want to have this structure:
[{ "name" : VARIABLE }]
and if "Linux" is selected, I want to have this structure:
[{ "name" : VARIABLE }, { "name" : SECOND_VARIABLE }]
Filling the variables is no issue at all, only "building" the dictionaries within if-clauses beforehand and moving them as variable in this structure is an issue.

I tried the following:

if var_LinuxDistro == 1:
  components_list = '{ "name" : "Linux" }, { "name" : "'+VARIABLE+'" }'

else:
  components_list = '{ "name" : "'+VARIABLE+'" }'
and took this components to the creation part:

issue_dict = {
    'project': {'key': 'Project'},
    'summary': 'Testing issue from Python',
    'description': 'Ticket description.',
    'issuetype': {'name': 'Issue'},
    'components': [components_list],
}
Leading to an error
response text = {"errorMessages":[],"errors":{"components":"expected Object"}}

How can I get this fixed?


If this is necessary, here is an example how to import an issue to JIRA with python

issue_dict = {
    'project': {'key': 'Project'},
    'summary': 'Testing issue from Python',
    'description': 'Ticket description.',
    'issuetype': {'name': 'Issue'},
    'components': [{'name': 'Issue'}, {'name': 'Issue'}],
}

new_issue = jira_connection.create_issue(fields=issue_dict)
Why are you building dictionaries inside strings instead of actually using dictionaries like the last code snippet you show does?
(Dec-24-2022, 09:35 AM)ndc85430 Wrote: [ -> ]Why are you building dictionaries inside strings instead of actually using dictionaries like the last code snippet you show does?

I am not sure if I get your intention. My intention was to have one defined structure and adapt the entries in one creation section.
- project and issuetype are fix values
- summary and description are covered via variables
- only the components are flexible in their structure

How can I implement my use case as dictionaries then as you mean it?
You seem to know how to create a dictionary, given that's what you're doing with issue_dict above.

Just do the same kind of thing for your components, don't create strings.

A string is not a dictionary. That's why you get the error you do (it's referring to objects because that's what dictionaries are called in JSON,which is the format used under the hood).
(Dec-24-2022, 09:52 AM)ndc85430 Wrote: [ -> ]You seem to know how to create a dictionary, given that's what you're doing with issue_dict above.

Just do the same kind of thing for your components, don't create strings.

A string is not a dictionary. That's why you get the error you do (it's referring to objects because that's what dictionaries are called in JSON,which is the format used under the hood).

I tried to create dictionaries within my if-clause using the "dict()" command. For single components it worked but for adding multiple components I get the error message that
Error:
"name" already exists
I also tried with json.loads(components_list) to kind of convert the string as json compatible (which might be non-sense :D )...having too many hours spent with this, I guess, I am on the hose :(
Can you show the code you're trying to use to create dictionaries? Please don't make us guess!

Literally you don't need to use json.loads. Again, for issue_dict above, you're just creating a literal dictionary, so you should be doing the same here.

I'm not at a computer right now, so typing code is not the easiest!
It looks like you are trying to create a list of one item dictionaries. The list may have 1 or 2 items. Is this correct?
(Dec-24-2022, 10:50 AM)ndc85430 Wrote: [ -> ]Can you show the code you're trying to use to create dictionaries? Please don't make us guess!

Literally you don't need to use json.loads. Again, for issue_dict above, you're just creating a literal dictionary, so you should be doing the same here.

I'm not at a computer right now, so typing code is not the easiest!

Well, as you might assume (and I need this structure with "name" : "component", I tried to build the dict with

thisdict = {
  "name": "Linux",
  "name": "Mint",
  }
print(thisdict)
(Dec-24-2022, 05:19 PM)jefsummers Wrote: [ -> ]It looks like you are trying to create a list of one item dictionaries. The list may have 1 or 2 items. Is this correct?

Yes, that's right. The components are to be provided as arrays. It should be kind of a "one dictionary entry array"?
Yes, the keys in s dictionary need to be unique. If you needed to have a list of dictionaries for JIRA, why not do that? You're literally doing it on line 6 in that snippet showing issue_dict.
(Dec-25-2022, 12:58 PM)ndc85430 Wrote: [ -> ]Yes, the keys in s dictionary need to be unique. If you needed to have a list of dictionaries for JIRA, why not do that? You're literally doing it on line 6 in that snippet showing issue_dict.

So you mean a list of dictionaries like

 components = [
     { "name": "Linux" },
     { "name": "Mint" }
 ]
Pages: 1 2