skip to Main Content

I need to get the values of ‘name’ from such string

[{'self': 'https://jira.zxz.su/rest/api/2/component/11207132',
  'id': '11207',    
  'name': 'SEO'},    
 {'self': 'https://jira.zxz.su/rest/api/2/component/12200123',    
  'id': '12200',    
  'name': 'НКК'}] 

I want to get an output like 'SEO','HKK' all in one string.

5

Answers


  1. This should work:

    data = [{'self': 'https://jira.zxz.su/rest/api/2/component/11207132',
      'id': '11207',    
      'name': 'SEO'},    
     {'self': 'https://jira.zxz.su/rest/api/2/component/12200123',    
      'id': '12200',    
      'name': 'НКК'}]
    
    print(','.join([f"'{x['name']}'" for x in data]))
    

    Output:

    "'SEO','НКК'"
    

    Explanation:
    The list comprehension creates a flat list comprised of values corresponding to the name key for every dictionary in data. Then we simply wrap those elements in quotes and join them in a comma-separated string.

    Login or Signup to reply.
  2. You have a list of dictionaries. All you need to do is to iterate over the dictionaries and to keep the name key of each one of them:

    entities = [{'self': 'https://jira.zxz.su/rest/api/2/component/11207132', 'id': '11207', 'name': 'SEO'},
                {'self': 'https://jira.zxz.su/rest/api/2/component/12200123', 'id': '12200', 'name': 'НКК'}]
    
    result = []
    for entity in entities:
        result.append(entity['name'])
    
    print(result)
    

    Output:

    ['SEO', 'НКК']
    
    Login or Signup to reply.
  3. If your input is not an array or dictionary, but a string you can not directly access the data. You can use json:

    import json
    def get_names(components: str):
        # prevent error: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
        components = components.replace(''', '"')
        # convert string to json
        components = json.loads(components)
        # create empty list
        names = []
        # iterate through components
        for component in components:
            # append name to list
            names.append(component['name'])
        # return list
        return names
    
    s = """[{'self': 'https://jira.zxz.su/rest/api/2/component/11207132',
      'id': '11207',    
      'name': 'SEO'},    
     {'self': 'https://jira.zxz.su/rest/api/2/component/12200123',    
      'id': '12200',    
      'name': 'НКК'}]"""
    print(get_names(s))
    

    Output:

    ['SEO', 'НКК']
    

    To get your requested Output you can do:

    print(', '.join(["'"+i+"'" for i in get_names(s)]))
    

    This will give you:

    'SEO', 'НКК'
    
    Login or Signup to reply.
  4. Depends on how you want your name data stored, but something like this will work:

    dicts = [
    {'self': 'https://jira.zxz.su/rest/api/2/component/11207132',
     'id': '11207',
     'name': 'SEO'},
    {'self': 'https://jira.zxz.su/rest/api/2/component/12200123',
     'id': '12200',
     'name': 'НКК'}
    ]
    
    names_only = [i['name'] for i in dicts] 
    
    Login or Signup to reply.
  5. This should extract and format the names as you indicate:

    dx = [{'self': 'https://jira.zxz.su/rest/api/2/component/11207132',
           'id': '11207',    
           'name': 'SEO'},    
          {'self': 'https://jira.zxz.su/rest/api/2/component/12200123',    
           'id': '12200',    
           'name': 'НКК'}] 
    
    names = ", ".join(["'{}'".format(item.get('name')) for item in dx])
    print(names)
    

    Putting this into the REPL shows:

    >>> print(names)
    'SEO', 'НКК'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search