Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

better way to access values in a json file than for loop?

I have a json file which looks like this:

[{'data': [{'text': 'add '},
  {'text': 'Stani, stani Ibar vodo', 'entity': 'entity_name'},
  {'text': ' songs in '},
  {'text': 'my', 'entity': 'playlist_owner'},
  {'text': ' playlist '},
  {'text': 'música libre', 'entity': 'playlist'}]},
{'data': [{'text': 'add this '},
  {'text': 'album', 'entity': 'music_item'},
  {'text': ' to '},
  {'text': 'my', 'entity': 'playlist_owner'},
  {'text': ' '},
  {'text': 'Blues', 'entity': 'playlist'},
  {'text': ' playlist'}]},
{'data': [{'text': 'Add the '},
  {'text': 'tune', 'entity': 'music_item'},
  {'text': ' to the '},
  {'text': 'Rage Radio', 'entity': 'playlist'},
  {'text': ' playlist.'}]}]

I want to append the values in ‘text’ for each ‘data’ in this list.

I have tried the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lst = []

for item in data:
    p = item['data']
    p_st = ''
    for item_1 in p:
        p_st += item_1['text'] + ' '
    lst.append(p_st)
    
print(lst)

Out: ['add  Stani, stani Ibar vodo  songs in  my  playlist  música libre ', 'add this  album  to  my   Blues  playlist ', 'Add the  tune  to the  Rage Radio  playlist. ']

It works but I am new to json and am wondering if there is a better way to do it? some in-build methods or libraries for json maybe? thank yuo.

>Solution :

Your code works well for extracting the text values from the JSON data. However, if you want a more concise way to achieve the same result, you can use list comprehensions in Python, which can make your code shorter and more readable. Here’s how you can do it:

Using JSON module and list comprehensions:

import json

data = [{'data': [{'text': 'add '}, {'text': 'Stani, stani Ibar vodo', 'entity': 'entity_name'}, {'text': ' songs in '}, {'text': 'my', 'entity': 'playlist_owner'}, {'text': ' playlist '}, {'text': 'música libre', 'entity': 'playlist'}]},
        {'data': [{'text': 'add this '}, {'text': 'album', 'entity': 'music_item'}, {'text': ' to '}, {'text': 'my', 'entity': 'playlist_owner'}, {'text': ' '}, {'text': 'Blues', 'entity': 'playlist'}, {'text': ' playlist'}]},
        {'data': [{'text': 'Add the '}, {'text': 'tune', 'entity': 'music_item'}, {'text': ' to the '}, {'text': 'Rage Radio', 'entity': 'playlist'}, {'text': ' playlist.'}]}]

text_values = [' '.join(item['text'] for item in entry['data']) for entry in data]

print(text_values)

Using pandas:

import pandas as pd

data = [{'data': [{'text': 'add '}, {'text': 'Stani, stani Ibar vodo', 'entity': 'entity_name'}, {'text': ' songs in '}, {'text': 'my', 'entity': 'playlist_owner'}, {'text': ' playlist '}, {'text': 'música libre', 'entity': 'playlist'}]},
        {'data': [{'text': 'add this '}, {'text': 'album', 'entity': 'music_item'}, {'text': ' to '}, {'text': 'my', 'entity': 'playlist_owner'}, {'text': ' '}, {'text': 'Blues', 'entity': 'playlist'}, {'text': ' playlist'}]},
        {'data': [{'text': 'Add the '}, {'text': 'tune', 'entity': 'music_item'}, {'text': ' to the '}, {'text': 'Rage Radio', 'entity': 'playlist'}, {'text': ' playlist.'}]}]

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Extract and join the 'text' values for each 'data' entry
text_values = df['data'].apply(lambda x: ' '.join(item['text'] for item in x))

print(text_values.tolist())

The pandas approach is more suitable if you plan to perform additional data analysis or manipulation on your JSON data, as it provides a powerful and flexible way to work with structured data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading