I would like to get data from a *.json file into file.txt
with batch.
My JSON file config.json
:
"nodes": [
{
"id": "item1",
"host": "${host:item1}",
"apps": [
{
"id": "value1",
"template_id": "value1"
},
{
"id": "value2",
"template_id": "value2",
},
I want to get only the values value1
and value2
of id
elements of node apps
. But the problem on using the command find
in my script is actually that it reads the values of id
and template_id
.
Is it possible to get the value to id
and not template_id
?
I tried this... still not working...
setlocal EnableDelayedExpansion
set c=0
for /f "tokens=2 delims=:, " %%a in (' find "id" ^< "config.json" ') do (
set /a c+=1
set val[!c!]=%%~a
)
for /L %%b in (1,1,!c!) do echo !val[%%b]!
And after that, I don't know really how to get all these data in my text file.
You shouldn't treat structured information as plain text.
If I take for example a Valid JSON (RFC 4627)
version of your fragment:
{
"nodes":[
{
"id":"item1",
"host":"${host:item1}",
"apps":[
{
"id":"value1",
"template_id":"value1"
},
{
"id":"value2",
"template_id":"value2"
}
]
}]
}
And use PowerShell's cmdlet ConvertFrom-Json
it's quite easy to receive the proper ID values
PoSh> $Json = Get-Content .\config.json | ConvertFrom-Json
PoSh> $JSon.Nodes.Apps.id
value1
value2
To be on topic with the batch-file tag wrapping this in a batch
@Echo off
Powershell -Nop -C "(Get-Content .\config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt