Search code examples
jsonshellshjq

How to convert output into JSON format?


I am trying to convert below output into a valid json output. Cant able to solve this using shell scripts.

output

svc pts/0        localhost. Tue Apr 28 21:40   still logged in   
svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)    
svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)
...etc

valid json output

{
    "Result": [{
        "models": ["svc pts 0 localhost.Tue Apr 28 21: 40 still logged in",
            "svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)"
        ]
    }]
}

is it possible to convert? can someone provide solution.s


Solution

  • You can use like:

    program | jq -Rn '{Result:[{models:[inputs]}]}'
    

    program being the program producing that output.