1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143

3 Answers3

1

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
  • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ? – programmation1994 Nov 24 '18 at 19:18
  • You' need to concatenate the information from different levels. `Powershell -Nop -C "(Get-Content .\config.json|ConvertFrom-Json).Nodes.id" >file.txt` and append the the other command to the file with `>>file.txt` –  Nov 24 '18 at 19:20
  • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ? – programmation1994 Nov 24 '18 at 19:42
  • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. `$json.nodes[1].id`, `$json.nodes[1].apps.id` –  Nov 24 '18 at 19:55
1

You can try with jsonextractor.bat . In order to have a valid json I've used this:

{
    "nodes": [{
        "id": "item1",
        "host": "${host:item1}",
        "apps": [{
                "id": "value1",
                "template_id": "value1"
            },
            {
                "id": "value2",
                "template_id": "value2"
            }
        ]
    }]
}

and to get the values:

for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
 set "first_id=%%~a"
)
echo %first_id%
Mofi
  • 46,139
  • 17
  • 80
  • 143
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

Please use a decent JSON parser, like Xidel, to parse JSON!

xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
value1
value2

To save the output to a file:

xidel -s config.json -e "$json/(.//apps)()/id" > output.txt
Reino
  • 3,203
  • 1
  • 13
  • 21