Résolu transformer un csv en json

Plus d'informations
il y a 3 ans 5 mois - il y a 3 ans 5 mois #30060 par wartraxx
Bonjour à tous,
Voici mon soucis j'ai un fichier csv de cette forme la

Key;Domain;currentIP
toto;crash.com;192.168.0.1
titi;crash.com;192.168.0.2
srv;test.com;10.133.40.10

j'essaie d'avoir ce rendu la
{
"zoneName": "crash.com",
"edits": [
{
recordType = A,
action = EDIT,
currentKey = toto,
currentValue = 192.168.0.2,
"newTtl": 300
},
{
recordType = A,
action = EDIT,
currentKey = titi,
currentValue = 192.168.0.1,
"newTtl": 300
}
]
}
AddressObjects = Import-Csv .test.csv -Delimiter ";"
$AddressObjects | foreach {
            $zoneName = $_.Domain
            $currentKey = $_.Key
            $CurrentIP = $_.currentIP
$Script = "
                {
                    `"zoneName`": `"$zoneName`",
                    `"edits`": [
                    {
                        `"recordType`": `"A`",
                        `"action`": `"EDIT`",
                        `"currentKey`": `"$currentKey`",
                        `"currentValue`": `"CurrentIP`",
                        `"newTtl`": 300
                    }
                    ]
                }"
echo "$Script"

le rendu que j'ai
{
"zoneName": "crash.com",
"edits": [
{
recordType = A,
action = EDIT,
currentKey = toto,
currentValue = 192.168.0.1,
"newTtl": 300
}
]
}
{
"zoneName": "crash.com",
"edits": [
{
recordType = A,
action = EDIT,
currentKey = titi,
currentValue = 192.168.0.2,
"newTtl": 300
}]
}
Je ne sais pas comment mettre une boucle sur les mêmes zones pour générer le fichier. Pouvez vous m'aider ?
Dernière édition: il y a 3 ans 5 mois par Arnaud Petitjean.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 3 ans 5 mois - il y a 3 ans 5 mois #30061 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet transformer un csv en json
Salut,
une possible approche :
$filepath='c:\temp\datas.csv'
@'
Key;Domain;currentIP
toto;crash.com;192.168.0.1
titi;crash.com;192.168.0.2
srv;test.com;10.133.40.10
'@ > $filepath

$ZoneGrp=import-csv $filepath -delimiter ';'|Group-Object -Property Domain
$ZoneGrp
# Count Name                      Group
# ----- ----                      -----
#     2 crash.com                 {@{Key=toto; Domain=crash.com; currentIP=192.168.0.1}, @{Key=titi; Domain=crash.com;...
#     1 test.com                  {@{Key=srv; Domain=test.com; currentIP=10.133.40.10}}

foreach ($Zone in $ZoneGrp)
{
    Write-Host "`r`nZone: $($Zone.name)"
    foreach ($Item  in $Zone.Group)
    { Write-Host "`tItem: $Item" }
}
# Zone: crash.com
#         Item: @{Key=toto; Domain=crash.com; currentIP=192.168.0.1}
#         Item: @{Key=titi; Domain=crash.com; currentIP=192.168.0.2}

# Zone: test.com
#         Item: @{Key=srv; Domain=test.com; currentIP=10.133.40.10}

Tutoriels PowerShell
Dernière édition: il y a 3 ans 5 mois par Laurent Dardenne.
Les utilisateur(s) suivant ont remercié: wartraxx

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 3 ans 5 mois #30079 par wartraxx
Réponse de wartraxx sur le sujet transformer un csv en json
Merci, ça marche :)

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.069 secondes
Propulsé par Kunena