'Getting all the Keys from JSON Object?

Goal: To create a script that will take in nested JSON object as input and output a CSV file with all keys as rows in the CSV?

Example:

{
    "Document": {
      "DocumentType": 945,
      "Version": "V007",
      "ClientCode": "WI",
      "Shipment": [
        {
          "ShipmentHeader": {
            "ShipmentID": 123456789,
            "OrderChannel": "Shopify",
            "CustomerNumber": 234234,
            "VendorID": "2343SDF",
            "ShipViaCode": "FEDX2D",
            "AsnDate": "2018-01-27",
            "AsnTime": "09:30:47-08:00",
            "ShipmentDate": "2018-01-23",
            "ShipmentTime": "09:30:47-08:00",
            "MBOL": 12345678901234568,
            "BOL": 12345678901234566,
            "ShippingNumber": "1ZTESTTEST",
            "LoadID": 321456987,
            "ShipmentWeight": 10,
            "ShipmentCost": 2.3,
            "CartonsTotal": 2,
            "CartonPackagingCode": "CTN25",
            "OrdersTotal": 2
          },
          "References": [
            {
              "Reference": {
                "ReferenceQualifier": "TST",
                "ReferenceText": "Testing text"
              }
            }
          ],
          "Addresses": {
            "Address": [
              {
                "AddressLocationQualifier": "ST",
                "LocationNumber": 23234234,
                "Name": "John Smith",
                "Address1": "123 Main St",
                "Address2": "Suite 12",
                "City": "Hometown",
                "State": "WA",
                "Zip": 92345,
                "Country": "USA"
              },
              {
                "AddressLocationQualifier": "BT",
                "LocationNumber": 2342342,
                "Name": "Jane Smith",
                "Address1": "345 Second Ave",
                "Address2": "Building 32",
                "City": "Sometown",
                "State": "CA",
                "Zip": "23665-0987",
                "Country": "USA"
              }
            ]
          },
          "Orders": {
            "Order": [
              {
                "OrderHeader": {
                  "PurchaseOrderNumber": 23456342,
                  "RetailerPurchaseOrderNumber": 234234234,
                  "RetailerOrderNumber": 23423423,
                  "CustomerOrderNumber": 234234234,
                  "Department": 3333,
                  "Division": 23423,
                  "OrderWeight": 10.23,
                  "CartonsTotal": 2,
                  "QTYOrdered": 12,
                  "QTYShipped": 23
                },
                "Cartons": {
                  "Carton": [
                    {
                      "SSCC18": 12345678901234567000,
                      "TrackingNumber": "1ZTESTTESTTEST",
                      "CartonContentsQty": 10,
                      "CartonWeight": 10.23,
                      "LineItems": {
                        "LineItem": [
                          {
                            "LineNumber": 1,
                            "ItemNumber": 1234567890,
                            "UPC": 9876543212,
                            "QTYOrdered": 34,
                            "QTYShipped": 32,
                            "QTYUOM": "EA",
                            "Description": "Shoes",
                            "Style": "Tall",
                            "Size": 9.5,
                            "Color": "Bllack",
                            "RetailerItemNumber": 2342333,
                            "OuterPack": 10
                          },
                          {
                            "LineNumber": 2,
                            "ItemNumber": 987654321,
                            "UPC": 7654324567,
                            "QTYOrdered": 12,
                            "QTYShipped": 23,
                            "QTYUOM": "EA",
                            "Description": "Sunglasses",
                            "Style": "Short",
                            "Size": 10,
                            "Color": "White",
                            "RetailerItemNumber": 565465456,
                            "OuterPack": 12
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }

In the above JSON Object, I want all the keys (nested included) in a List (Duplicates can be removed by using a set Data Structure). If Nested Key Occurs like in actual JSON they can be keys multiple times in the CSV !



Solution 1:[1]

I personally feel that recursion is a perfect application for this type of problem if the amount of nests you will encounter is unpredictable. Here I have written an example in Python of how you can utilise recursion to extract all keys. Cheers.

import json

row = ""

def extract_keys(data):
    global row

    if isinstance(data, dict):
        for key, value in data.items():
            row += key + "\n"
            extract_keys(value)

    elif isinstance(data, list):
        for element in data:
            extract_keys(element)

# MAIN
with open("input.json", "r") as rfile:
    dicts = json.load(rfile)

extract_keys(dicts)

with open("output.csv", "w") as wfile:
    wfile.write(row)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CherrieP