12

I have this JSON file called test.json which says:

{
    "test":true,
    "limit": 
    {
        "min":0.5,
        "max":1.5
    }
}

I'd like to be able to read this file in the Windows command line and have these objects parsed as variables.

How would I do this?

cyroxx
  • 3,809
  • 3
  • 23
  • 35
Kriem
  • 8,666
  • 16
  • 72
  • 120

3 Answers3

11

If you use Windows Powershell as your command line, you can use the ConvertFrom-JSON cmdlet: http://powershelljson.codeplex.com/

Make sure your PowerShell version is above 3.0, as ConvertFrom-JSON is available from that version.

If you use plain old CMD, you'll need an external tool for it. I like jq: http://stedolan.github.io/jq/. The tutorial uses curl for the examples, but you can just as easily use echo or read the JSON from a file.

Community
  • 1
  • 1
Idan Arye
  • 12,402
  • 5
  • 49
  • 68
  • I'm afraid I can't use Powershell. – Kriem Oct 14 '13 at 08:43
  • 1
    See the second part of my answer – Idan Arye Oct 14 '13 at 08:47
  • I'm not sure how this would help me create variables. Am I missing something? – Kriem Oct 14 '13 at 09:03
  • CMD variables are strings - the only way for them to hold complex hierarchical structures is if those structures are serialized. So, whenever you need a specific value from the JSON, you stream the variable through `jq` with a selector to select what you need. – Idan Arye Oct 14 '13 at 09:34
  • 1
    We have the exact same reputations core btw :P – Kriem Oct 14 '13 at 10:43
  • Quick question: how would I set a variable to the output of this? Say, I have limit.min, which outputs 0.5. How would I set it to a variable? – Kriem Oct 15 '13 at 07:37
  • 6
    To answer my own question: `FOR /F "delims=" %%i in ('type test.json ^| jq .limit.min') DO SET min=%%i` – Kriem Oct 15 '13 at 07:50
  • 4
    Note that you don't need to use `type` here - you can use the file redirection operator `<`, and write `'jq .limit.min ^< test.json'`. – Idan Arye Oct 15 '13 at 07:54
1

PowerShell example. Create from file: $js = Get-Content file.json | ConvertFrom-Json. Get subkey: $js.key.subkey

0

I'd like to be able to read this file in the Windows command line and have these objects parsed as variables.

I'll interpret this as, you want to do:

SET min=0.5
SET max=1.5

I'd recommend the powerful tool xidel for this task.

Basic command:

xidel -s test.json -e "$json/limit/min" -e "$json/limit/max"
xidel -s test.json -e "$json/limit/(min,max)"

Whether you use 2 queries to return each value, or 1 query to return both values, both commands should return:

0.5
1.5

Export with custom variable-names:

FOR /F "delims=" %A IN ('xidel -s test.json -e "min:=$json/limit/min" -e "max:=$json/limit/max" --output-format^=cmd') DO %A
FOR /F "delims=" %A IN ('xidel -s test.json -e "$json/limit/(min:=min,max:=max)" --output-format^=cmd') DO %A

Both commands do:

SET min=0.5
SET max=1.5

Export with key-names as variable-names:

FOR /F "delims=" %A IN ('xidel -s test.json -e "$json/(limit)() ! eval(x'{.}:=$json/limit/{.}')[0]" --output-format^=cmd') DO %A

To answer my own question: FOR /F "delims=" %%i in ('type test.json ^| jq .limit.min') DO SET min=%%i

If that's all you want, then...

FOR /F "delims=" %A IN ('xidel -s test.json -e "$json/limit/min"') DO SET min=%A

or...

FOR /F "delims=" %A IN ('xidel -s test.json -e "min:=$json/limit/min" --output-format^=cmd') DO %A

...is all you need.

Reino
  • 3,203
  • 1
  • 13
  • 21