0

I would like to manipulate the content of jason file. I've tried with powershell or linux bash but I was unable to get what I want. On linux, I was thinking to use jq tool, despite obtains data, I cannot manipulate them.

jq '.[].pathSpec, .[].scope' jasonfilepath

Current output:

"file"
"file"
"/u01/app/grid/*/bin/oracle"
"/u01/app/oracle/product/*/db_1/bin/oracle"

My goal is to obtain something similar as:

scope pathSpec

Like:

file /u01/app/grid/*/bin/oracle
file /u01/app/oracle/product/*/db_1/bin/oracle

Jason file sample

[
    {
        "actions": [
            "upload",
            "detect"
        ],
        "deep": false,
        "dfi": true,
        "dynamic": true,
        "inject": false,
        "monitor": false,
        "pathSpec": "/u01/app/grid/*/bin/oracle",
        "scope": "file"
    },
    {
        "actions": [
            "upload",
            "detect"
        ],
        "deep": false,
        "dfi": true,
        "dynamic": true,
        "inject": false,
        "monitor": false,
        "pathSpec": "/u01/app/oracle/product/*/db_1/bin/oracle",
        "scope": "file"
    }
]

Do you have any idea to get this kind of expected output in Powershell and bash?

Thanks by advance,

LEFBE
  • 125
  • 1
  • 9
  • 1
    Is this question about JSON (JavaScript Object Notation-encoded data) or Jason (a distinct superset of JSON specifically for serializing executable code)? – Mathias R. Jessen Aug 16 '22 at 12:16
  • 1
    Assuming your JSON string is in a variable called `$json` in PowerShell you could do this: `$json | ConvertFrom-Json | %{"$($_.scope) $($_.pathSpec)"}` – JohnLBevan Aug 16 '22 at 12:18
  • 1
    Please don't ask two things in one question. Focus on one language (Powershell **or** bash). If you need it implemented in two languages (why?), try to translate it by yourself. If you get stuck, ask a **new** question. – user1934428 Aug 16 '22 at 12:46
  • In bash, try jq '.[].scope+ " " +.[].pathSpec' jasonfilepath – Raman Sailopal Aug 16 '22 at 13:24

1 Answers1

0

Assuming a JSON input file named file.json:

In a Linux / Bash environment, use the following:

jq -r '.[] | .scope + " " + .pathSpec' file.json

In PowerShell, use the following (adapted from a comment by JohnLBevan):

(Get-Content -Raw file.json | ConvertFrom-Json) | 
  ForEach-Object { '{0} {1}' -f $_.scope, $_.pathSpec }

Note the (...) around the pipeline with the ConvertFrom-Json call, which is necessary in Windows PowerShell (but no longer in PowerShell (Core) 7+) to ensure that the parsed JSON array is enumerated in the pipeline, i.e. to ensure that its elements are sent one by one - see this post for more information.

mklement0
  • 382,024
  • 64
  • 607
  • 775