0
$\begingroup$

In my column with json data, I have this list I want to extract to column:

"list":[

{
    "id":"list",
    "item":[
        {
            "value":"Hergestellt in Italien aus 100% reinem Platin-Flüssigsilikon"
        },
        {
            "value":"Geruchs- und geschmacksneutral"
        },
        {
            "value":"Kältebeständig bis -60°C"
        },
        {
            "value":"Inklusive Rezeptbuch und 50 Eisstielen"
        },
        {
            "value":"spülmaschinengeeignet"
        }
    ],
    "decorators":[
    ]
},

I have extracted other data using this code but the value was string and not a dictionary:

if 'item' in item and item['item']:
                    if isinstance(item['item'], str):
                        cur_model_info[item['id']] = item['item']
                    elif isinstance(item['item'], list):
                        elements = [element['value'] for element in item['item']]
cur_model_info[item['id']] = ','.join(elements)

I tried to use this for the above format of data but I got this error: TypeError: sequence item 0: expected str instance, dict found

What should I change in order to be able to export in a separate column the data having each element from the list in a new cell of that column?

expected output

enter image description here

Using the above code, the data is exported in this format

![enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ Given the above json data, which values do you want to extract and what do you expect as the output? $\endgroup$ – Oxbowerce Jan 29 at 14:52
  • $\begingroup$ @Oxbowerce I want to extract the values after "value":(Hergestellt in Italien aus 100% reinem Platin-Flüssigsilikon and the rest of them) in a column, information present after "value" in a separate cell. Given the above example, I should have a column with information for 5 cells $\endgroup$ – Isa Jan 29 at 14:54
  • 1
    $\begingroup$ And what is the type of the original data? A dictionary or a list? $\endgroup$ – Oxbowerce Jan 29 at 15:04
  • $\begingroup$ do you json-decode the input or do you handle it as string? it is not clear from your post $\endgroup$ – Nikos M. Jan 29 at 18:10
  • $\begingroup$ @Oxbowerce I use json data and json.loads for handling data with python $\endgroup$ – Isa Jan 29 at 20:33
0
$\begingroup$

Given that the data snippet you provided is not a valid json structure I assume that the data is a list with dictionaries in it. Using this assumption, you can extract the values to a nested list using a list comprehension:

data = [
    {
    "id":"list",
    "item":[
        {
            "value":"Hergestellt in Italien aus 100% reinem Platin-Flüssigsilikon"
        },
        {
            "value":"Geruchs- und geschmacksneutral"
        },
        {
            "value":"Kältebeständig bis -60°C"
        },
        {
            "value":"Inklusive Rezeptbuch und 50 Eisstielen"
        },
        {
            "value":"spülmaschinengeeignet"
        }
    ],
    "decorators":[]
    }
]

print([[i["value"] for i in x["item"]] for x in data])

# [['Hergestellt in Italien aus 100% reinem Platin-Flüssigsilikon',
#  'Geruchs- und geschmacksneutral',
#  'Kältebeständig bis -60°C',
#  'Inklusive Rezeptbuch und 50 Eisstielen',
#  'spülmaschinengeeignet']]
$\endgroup$
12
  • $\begingroup$ thank you! The data is in json format,I posted just the part that need to be extracted and which is in a dictionary format. My code worked for cases where the value was a string, all that needs to be changed, is the last line so that the values can be exported on rows, meaning this line cur_model_info[item['id']] = ','.join(item['item']) I have changed it to elements = [element['value'] for element in item['item']] and added cur_model_info[item['id']] = ','.join(elements) which, exports the list of values but in one single row. $\endgroup$ – Isa Jan 29 at 21:41
  • $\begingroup$ So if I understand it correctly the issue is solved? $\endgroup$ – Oxbowerce Jan 29 at 21:49
  • $\begingroup$ unfortunately not, I have modified my code in the description. Is exporting the values not but all in one cell. Do you have any idea on how I should modify the last line of code so that I can export it on multiple rows? From my example of data should be 5 rows, one for each value. Thank you so much for taking your time $\endgroup$ – Isa Jan 29 at 21:52
  • $\begingroup$ If possible, please provide a full data extract as this makes it easier to debug. When you say cell/row, what exactly do you mean? Cells and rows are not a data structure within native python. $\endgroup$ – Oxbowerce Jan 29 at 21:55
  • $\begingroup$ I have updated the description with the expected format result. $\endgroup$ – Isa Jan 29 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.