0

My nested object/dictionary looks like the following. All the hierarchal fields would have the parent name added as the first value in the column. So for instance "properties" { "displayOnlyNewOrChangedRecords": false} would be a column with properties_displayOnlyNewOrChangedRecords as the column name. Similarly, for query, I would have query_fields, query_filter, and query_sortBy_fieldId as separate columns. Any way to do it without hardcoding the data?

{
  "description": "",
  "id": "1",
  "name": "List All",
  "properties": {
    "displayOnlyNewOrChangedRecords": false
  },
  "query": {
    "fields": [],
    "filter": "",
    "formulaFields": [],
    "groupBy": [],
    "sortBy": [
      {
        "fieldId": 6,
        "order": "DESC"
      }
    ],
    "tableId": "bsjuwuqzy"
  },
  "type": "table",
  "usedCount": 2,
  "usedLast": "2022-08-09T16:32:00Z"
}

How can I store the above mentioned object as a csv using python? Thank you!

Eliot Kim
  • 63
  • 8
  • 2
    CSV files store tabular data, not hierarchical data. You'll need to figure out how to represent your hierarchical data in tabular form. – chepner Aug 09 '22 at 17:20
  • Any ideas on how I can represent the above mentioned as tabular form? That's what I need help on. – Eliot Kim Aug 09 '22 at 17:21
  • Imagine if I just want the parent class as the first name of the column with the child component. So query { fields: [], filter: "" ...} would become query-fields as column and query-filter as column and so on. – Eliot Kim Aug 09 '22 at 17:24
  • The commas in your dictionary will break your csv. – Jortega Aug 09 '22 at 17:25
  • Can you [pickle](https://docs.python.org/3/library/pickle.html) it instead? – BERA Aug 09 '22 at 17:30
  • Perhaps it is better to use a binary format, such as [pickle](https://docs.python.org/3/library/pickle.html) or create a [json](https://docs.python.org/3/library/json.html), which usually seems like the natural representation of a python dictionary – max Aug 09 '22 at 17:33
  • Why not just store it in json form. What is forcing you to store this data as csv format? unless this is some homework question then I see no reason why you would want to store this data in csv format. Other than trying to load it in something like excel and if so then please specify instead of repeatedly asking for the same thing ignoring our suggestions – Jab Aug 09 '22 at 17:34
  • 1
    It sounds like you are looking for an excuse to use CSV rather than trying to solve an actual problem. – chepner Aug 09 '22 at 17:35
  • How would I store it as json? Simply writing it in a text file? – Eliot Kim Aug 09 '22 at 17:37
  • @EliotKim start by taking a look at the [json](https://docs.python.org/3/library/json.html) library – Jab Aug 09 '22 at 17:40

0 Answers0