Using the key from a nested CSV file as the primary key in Python

Sorry for the confusing title.

I have a csv file with rows structured like this:

1234567, Install X Software on Y Machine, ServiceTicket,"{'id': 47, 'name': 'SERVICE BOARD', '_info': {'board_href': 'https://MY-URl'}}","{'id': 897, 'name': 'Completed', '_info': {'status_href': 'https://MY-URl'}}...

The goal is to reformat it so that 'name' values under 'board' and 'status' keys are used instead. The desired format is:

1234567, Install X Software on Y Machine, ServiceTicket, SERVICE BOARD, Completed

I'm struggling with this task and would greatly appreciate any guidance.

Here's the code I've written to extract .json data from a GET request and convert it into a .csv file:

data = requests.get(url=TICKET_URL, headers=HEADER_AUTH) ### Get data from
data = data.json()
count = 0;
csvData = open("test.csv", "w")
csvWriter = csv.writer(csvData)
with open("data.json", "w") as f:
        for item in data:
            if count == 0:
                header = item.keys()
                csvWriter.writerow(header)
                count += 1
            csvWriter.writerow(item.values())
            f.write("%s\n" % item)
csvData.close()
f.close()

Answer №1

This solution may not be optimal, but it should get the job done. You might also want to consider using `itemgetter`.

with open('data.csv', 'w') as file:
    writer = csv.DictWriter(file, fieldnames=('foo1', 'foo2'))
    writer.writeheader()
    for row in data:
        record = []
        for item in row.values():
            if isinstance(item, dict):
                record.append(item['name'])
            else:
                record.append(item)
        writer.writerow(record)

Answer №2

To obtain the desired layout, I first flattened the JSON file before converting it into CSV and then removing unnecessary keys. While not a perfect solution, it gets the job done effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experimenting with Selenium Webdriver to locate an input element by its name and input a specific value

I'm having trouble inputting a value in the final box. (Chassis Number) Here's what I've attempted: python from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by imp ...

Efficient method for reading intricate data structures from disk in Python

My CSV dataset contains strings in a single field that represent lists of values. The sequences vary greatly in length, ranging from one observation to thousands. I am currently parsing these strings into nested lists within a Pandas DataFrame. Although th ...

Gathering information from various web pages simultaneously without the need to manually navigate through each page using a webdriver

I'm new to the world of web scraping and I've successfully developed a program that allows me to extract specific, dynamic data by utilizing the selenium web driver. My current project involves scraping data from a FAQ page in order to gather in ...

Guide to creating targeted requests with Wiremock

Working on simulating mock data with Wiremock, I am sending a JSON body request as follows: {"petId ":"123"} For petIds 123, 124, and 125, the expected response should be: {"petType":"1", "wildLevel":"40"} For petIds 250, 251, and 252, {"petTyp ...

AngularJS - Import and save CSV files

I have set up a nodeJS program as the server and an AngularJS web application as the client. For generating CSV files, I am utilizing the "express-csv" library (https://www.npmjs.com/package/express-csv) Below is the code for the server side: Definition ...

Is it possible to accept a number in string format in the request body?

I have a specific API spec that outlines the parameter account_id as being a string within the request body. If a request is received with a value for this field either as a number or even a boolean, such as: "account_id": 1234567890 or "account_id": t ...

JSON syntax error: "r" is not a valid token at the beginning position

Currently, I am in the process of developing a web server that is based on STM32 MCU. The workflow involves the browser sending a request to the MCU, which responds with a web HTML file. Users can then adjust parameters and use a form to submit them back t ...

Parsing through a deeply nested JSON array with PHP

Here is my JSON array: [ { "custClass": [ { "code": "50824109d3b1947c9d9390ac5caae0ef", "desc": "e1f96b98047adbc39f8baf8f4aa36f41" }, { "code": "dab6cc0ed3688f96333d91fd979c5f74", ...

Exploring the Power of SQL in JSON Parsing (OPENJSON)

If you want to access your Google searches data, you can download it in the form of multiple JSON files. I am currently working on parsing them into columns named [TimeStamp] and [Query Text] using the SQL function OPENJSON. DECLARE @json as nvarchar(max) ...

Utilizing angularjs ng-repeat directive to present JSON data in an HTML table

I've been struggling to showcase the JSON data in my HTML table using AngularJS ng-repeat directive. Here's the code snippet: <thead> <tr> <th ng-repeat="(header, value) in gridheader">{{value}}</th> </tr> </ ...

Having finished coding my tic tac toe game, I find that it is error-free but for some reason it fails

This is my initial project where I have utilized various resources to come up with the code presented here. Utilizing Jupyter notebook, I have managed to resolve all error messages in the code, but unfortunately, I am facing difficulties running it success ...

Something went wrong while attempting to decode JSON in Python due to a missing comma delimiter

How can I send JSON data using sockets in Python? {"receiver": "2", "sender:": 1, "seq_num": 10, "data": "{"iv": "jdjhvwGriJ95kZwgDWlShw==", "ciphertext": "Fg7ugYYAn ...

Error: Trying to access an index of an integer data type in Python is not allowed

I am currently working on a project to create a simple program that selects a random website and then counts the elements within that website. However, I have encountered an error: Traceback (most recent call last): File "element_counter.py", line 23, ...

encountering issues while Deserializing a collection containing children objects of various types

I am facing an issue with deserializing a JSON packet that consists of a header (under PACKET_HEADER) and several different types of messages (under DATA). These messages have a parent class in common: //parent message class public class Message { pu ...

When outputting the $http response in the console, Angular displays null instead of the expected result,

I have encountered a peculiar issue with my local webservice developed using Spring. Everything seems to be functioning correctly when accessing it through the browser or Postman, but for some reason, when attempting a simple GET method with Angular/Ionic, ...

Can you explain the distinction between a threaded and a non-threaded Win10Toast notification within a Python context?

Exploring the win10toast documentation reveals two examples of notifications: from win10toast import ToastNotifier toaster = ToastNotifier() toaster.show_toast("Greetings Earthlings!", "Python is simply splendid in 10 seconds!", icon_path="custom ...

Python's ability to nest objects within other objects allows for complex data

I had an interesting experience during a quiz in my class. One of the questions stated that an object in Python cannot contain other objects within itself, which didn't quite add up to me. After all, why couldn't there be a class table and a clas ...

I am puzzled by this error in Typescript: "Why does the element have an 'any' type when the Object type lacks an index signature?"

Looking to extract an array of keys from an object with nested properties, my current code: public static getKeys(obj: Object) { let keys: string[] = []; for (let k in obj) { if (typeof obj[k] == "Object" && obj[k] !== null) { ...

Author: Facing difficulty in downloading dependency on Windows Azure platform

My composer.json file looks like this: 1 { 2 "require": { 3 "microsoft/windowsazure": "*" 4 }, 5 "repositories": [ 6 { 7 "type": "pear", 8 "url": "http://pear.php.net" ...

Generating a JSON response on the server side

Calling all talented programmers! Firstly, I want to mention that I have no prior experience working with JSON and minimal knowledge of server-side coding. What I am aiming for is quite simple: I would like to create a basic Backend server using either Ja ...