user3022917
user3022917

Reputation: 617

Extracting data from a JSON response in Python and exclude also specific data

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!

Upvotes: 1

Views: 98

Answers (1)

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11083

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

Upvotes: 1

Related Questions