0

I have a gigantic json file that I need to convert only some of the object keys into a csv. Here is a sample of what the json looks like:

enter image description here

Here is also some formatted raw sample data and not just a screenshot:

    {
  "1234567890": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  },
  "2345678912": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  },
  "3456789012": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  }
}

I'm wondering if anyone can point me in the right direction of the best way to create a csv that creates 4 columns with the highlighted values highlighted in the screenshot above? So the output I would be looking for would be this:

enter image description here

Can I do this with Node.js? Maybe some other library that does exactly this? A job for python? I prefer some kind of Javascript solution but am open to any suggestions.

Thank you!

3
  • What is the expected output? Commented Aug 4, 2021 at 17:59
  • 1
    It seems fairly simple to script yourself. iterate over all record keys, then all cards within a key, and for every record-card combo, output a line of text that includes the things you care about. Commented Aug 4, 2021 at 18:01
  • You say it's gigantic. Will it fit into RAM all at once? Commented Aug 4, 2021 at 19:39

2 Answers 2

2

Try this code

source = {
  "1234567890": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  },
  "2345678912": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  },
  "3456789012": {
    "id": "sdggdfgfhfgdhfg",
    "cards": {
      "252632123": {
        "id": "id_554874455544456646",
        "fingerprint": "fsadljfhnsdikiubcb",
        "last4": "5555",
        "exp_month": 12,
        "exp_year": 2024,
        "brand": "Visa"
      }
    }
  }
}

for key in source:
   record = []
   record.append(key)
   record_id = list(source[key]['cards'].keys())[0]
   record.append(record_id)
   exp_m = source[key]['cards'][record_id]['exp_month']
   exp_y = source[key]['cards'][record_id]['exp_year']
   record.append(exp_m)
   record.append(exp_y)
   print(record)
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a CSV package on NPM, or emit the CSV yourself. In both cases, you'll have to iterate over your array to decide what to emit (either by storing it in an array to pass to the module, or directly emit a line to the output file). Iterating over your data would look like this:

const data = {
    "1234567890": {
        "id": "sdggdfgfhfgdhfg",
        "cards": {
            "252632123": {
                "id": "id_554874455544456646",
                "fingerprint": "fsadljfhnsdikiubcb",
                "last4": "5555",
                "exp_month": 12,
                "exp_year": 2024,
                "brand": "Visa"
            }
        }
    },
    "2345678912": {
        "id": "sdggdfgfhfgdhfg",
        "cards": {
            "252632123": {
                "id": "id_554874455544456646",
                "fingerprint": "fsadljfhnsdikiubcb",
                "last4": "5555",
                "exp_month": 12,
                "exp_year": 2024,
                "brand": "Visa"
            }
        }
    },
    "3456789012": {
        "id": "sdggdfgfhfgdhfg",
        "cards": {
            "252632123": {
                "id": "id_554874455544456646",
                "fingerprint": "fsadljfhnsdikiubcb",
                "last4": "5555",
                "exp_month": 12,
                "exp_year": 2024,
                "brand": "Visa"
            }
        }
    }
};

for (const record_id in data) {
    const { cards } = data[record_id];
    for (const card_id in cards) {
        const { exp_month, exp_year } = cards[card_id];
        console.log({ record_id, card_id, exp_month, exp_year });
    }
}

This spits out a single row for every card, including the record_id it's part of. At that point, format as CSV and write to file or pass it to a module that formats it as CSV for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.