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.

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

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 ?
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
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 ?
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
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%

Comments

0

Please use a dedicated JSON-parser, like :

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

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.