1

I have following json_data in my Python shell:

[{u'alt_name': u'JON~1.EXT',
u'attributes': [u'DIRECTORY'],
u'create_time': 1538729344,
u'filename': u'Jon.Doe',
u'last_access_time': 1539071386,
u'last_write_time': 1539071386,
u'size': 0},

{u'attributes': [u'READONLY', u'DIRECTORY'],
u'create_time': 1536996830,
u'filename': u'Public',
u'last_access_time': 1538765229,
u'last_write_time': 1538765229,
u'size': 0},

{u'alt_name': u'ABC_IN~1',
u'attributes': [u'DIRECTORY'],
u'create_time': 1538729345,
u'filename': u'abc_inventory',
u'last_access_time': 1538729531,
u'last_write_time': 1538729531,
u'size': 0}]

I'm interested only in "filenames" and I would like to exclude "Public"

Goal:
Jon.Doe
abc_inventory

Thanks in advance for your assistance!

1 Answer 1

1

Try this:

json_data = [
     {u'alt_name': u'JON~1.EXT',
      u'attributes': [u'DIRECTORY'],
      u'create_time': 1538729344,
      u'filename': u'Jon.Doe',
      u'last_access_time': 1539071386,
      u'last_write_time': 1539071386,
      u'size': 0},

     {u'attributes': [u'READONLY', u'DIRECTORY'],
      u'create_time': 1536996830,
      u'filename': u'Public',
      u'last_access_time': 1538765229,
      u'last_write_time': 1538765229,
      u'size': 0},

     {u'alt_name': u'ABC_IN~1',
      u'attributes': [u'DIRECTORY'],
      u'create_time': 1538729345,
      u'filename': u'abc_inventory',
      u'last_access_time': 1538729531,
      u'last_write_time': 1538729531,
      u'size': 0}]


filenames = [i['filename'] for i in json_data]

the output will be:

['Jon.Doe', 'Public', 'abc_inventory']

if the logic is that to not get the Public:

filenames = [i['filename'] for i in json_data if i['filename'] !='Public']

the output will be:

['Jon.Doe', 'abc_inventory']
Sign up to request clarification or add additional context in comments.

1 Comment

filenames = [i['filename'] for i in json_data if i['filename'] !='Public']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.