How to convert a JSON list into two different columns in a CSV file?

Viewed 387

This is my first question on this spectacular website, I need to know how to export complex information from a JSON to a CSV. The problem is that I need from the list that I have in the column to have two different values.

I tried a lot of different combinations and I couldn't so one of my last resources are asked to the community. My code is this:

def output(alerts):
        output = list()
        for alert in alerts:
            applications = alerts['applications']
            for app in applications:
                categories = app['categories']
                for cat in categories:
                    output_alert = [list(cat.items())[0], app['confidence'], app['icon'],
                                    app['name'], app['version'], app['website'], alerts['language'], alerts['status']]
                    output.append(output_alert)

        df = pd.DataFrame(output, columns=['Categories', 'Confidence', 'Icon', 'Name', 'Version', 'Website',
                                           'Language', 'Status'])
        df.to_csv(args.output)
        print('Scan completed, you already have your new CSV file')
        return

enter image description here

I left you a picture of the CSV file with the problem in column B (I have a list there) but I need actually two columns with each value...

I attached the JSON response that I have from a REST API

{'applications': [{'categories': [{'59': 'JavaScript libraries'}],
               'confidence': '100',
               'icon': 'Lo-dash.png',
               'name': 'Lodash',
               'version': '4.17.15',
               'website': 'http://www.lodash.com'},
              {'categories': [{'12': 'JavaScript frameworks'}],
               'confidence': '100',
               'icon': 'RequireJS.png',
               'name': 'RequireJS',
               'version': '2.3.6',
               'website': 'http://requirejs.org'},
              {'categories': [{'13': 'Issue trackers'}],
               'confidence': '100',
               'icon': 'Sentry.svg',
               'name': 'Sentry',
               'version': '4.6.2',
               'website': 'https://sentry.io/'},
              {'categories': [{'1': 'CMS'},
                              {'6': 'Ecommerce'},
                              {'11': 'Blogs'}],
               'confidence': '100',
               'icon': 'Wix.png',
               'name': 'Wix',
               'version': None,
               'website': 'https://www.wix.com'},
              {'categories': [{'59': 'JavaScript libraries'}],
               'confidence': '100',
               'icon': 'Zepto.png',
               'name': 'Zepto',
               'version': None,
               'website': 'http://zeptojs.com'},
              {'categories': [{'19': 'Miscellaneous'}],
               'confidence': '100',
               'icon': 'webpack.svg',
               'name': 'webpack',
               'version': None,
               'website': 'https://webpack.js.org/'},
              {'categories': [{'12': 'JavaScript frameworks'}],
               'confidence': '0',
               'icon': 'React.png',
               'name': 'React',
               'version': None,
               'website': 'https://reactjs.org'}],  'language': 'es',  'status': 'success'}

[{'59': 'JavaScript libraries'}] this last thing is my big problem! Thank you for your time and help!

2 Answers

You could try using list(cat.keys())[0], list(cat.values())[0] in your output_alert variable to extract key and value separately.

You can use json_normalize to extract your columns without the for-loop, and then create two new columns with the extracted keys and values from categories:

result = pd.json_normalize(
    alerts,
    record_path=["applications"],
    meta=["language", "status"]
).explode("categories")

result["category_labels"] = result.categories.apply(lambda x: list(x.keys())[0])
result["category_values"] = result.categories.apply(lambda x: list(x.values())[0])

The output is: enter image description here

Related