skip to Main Content

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:

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. This will work:

    with open(filename,'r+') as file:
        #open and load json file into dict
        file_data = json.load(file)
        #append new data to dict
        file_data[].append(new_data)
        #sets file's current position at offset
        file.seek(0)
        #convert back to json
        json.dump(file_data, file, indent = 4)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search