I am writing my own API to talk to CyberArk's API. I have one part complete where it imports a text file containing a list of safes per each line.
Example: "safeListFromCyberArk.txt"
- PasswordManager
- Test002
- Test003
The method opens the txt file and reads each line to a string array. Each safeName from the txt file, is added into a list to retrieve account details based on that safeName.
public async Task<List<Accounts>> GetAccountDetailsFromSafeList()
{
List<Accounts> acc = new List<Accounts>();
foreach (var line in File.ReadLines("EPV-Data\\safeListFromCyberArk.txt"))
{
string[] safesInFile = line.Split('\t');
var accounts = GetAccountDetailsFromSafe(safesInFile[0]);
foreach (var account in await accounts)
{
acc.Add(new Accounts
{
id = account.id,
name = account.name,
address = account.address,
safeName = account.safeName,
userName = account.userName,
password = account.password
});
}
}
//var safeCount = acc.GroupBy(x=>x.safeName).Count();
//var ser = JsonConvert.SerializeObject(acc.GroupBy(x=>x.safeName));
//File.WriteAllText($"EPV-Data\\{WOULD LIKE SAFE NAME AS THE FILENAME}.json", ser);
return acc;
}
When I inspect the line...
var ser = JsonConvert.SerializeObject(acc.GroupBy(x=>x.safeName));
I can see that "ser" contains 3 JSON arrays, the first array, shows 3 accounts, and the other two arrays show 1 account each, which is correct, as it is grouped by the safeName.
What I'm trying to accomplish is, saving individual files with the account details, with the filename being the name of the current safeName that the array comes from. So my expected result would be:
"PasswordManager.json" - which would contain the JSON array for the 3 accounts for the safe "PasswordManager"
"Test002.json" - containing JSON array of 1 account for the safe "Test002"
"Test003.json" - containing JSON array of 1 account for the safe "Test003"
However, I am at a loss on how to do it. I'm assuming I would need to iterate over the arrays with a for loop and in there I would do the File.WriteAllText(...), but just not sure.
Any help would be appreciated.
Thanks