0

I'm extracting each user's data into m4-$u.json file with the below shell script

#!/bin/bash

USERID=ricardo.sanchez
PASSWORD=password
PORT=2728

for u in `cat user-list.txt`;
   do echo $u; 
   curl --user $USERID:$PASSWORD http://198.98.99.12:46567/$PORT/protects/$u | jq '.' > m4-$u.json
done 

One for the user's output m4-daniel.json file. From that few lines as follows.

[
  {
    "depotFile": "//ABND/JJEB/...",
    "host": "*",
    "isgroup": "",
    "line": "16",
    "perm": "open",
    "user": "5G_USER_GROUP"
  },
  {
    "depotFile": "//LIB/...",
    "host": "*",
    "isgroup": "",
    "line": "19",
    "perm": "write",
    "user": "6G_USER_GROUP"
  },
  {
    "depotFile": "//AND/RIO/...",
    "host": "*",
    "isgroup": "",
    "line": "20",
    "perm": "write",
    "user": "AND_USER_GROUP"
  },

Now from the json output files, I need covert to get it in excel (or) csv in below format. Additionally $PORT & $U shell variables need to be present in the excel (or) csv file. Any help will be appreciated.

expected output

1 Answer 1

1

See below (a csv file is generated). Note that there is no $PORT and $U in the data.

data = [
    {
        "depotFile": "//ABND/JJEB/...",
        "host": "*",
        "isgroup": "",
        "line": "16",
        "perm": "open",
        "user": "5G_USER_GROUP"
    },
    {
        "depotFile": "//LIB/...",
        "host": "*",
        "isgroup": "",
        "line": "19",
        "perm": "write",
        "user": "6G_USER_GROUP"
    },
    {
        "depotFile": "//AND/RIO/...",
        "host": "*",
        "isgroup": "",
        "line": "20",
        "perm": "write",
        "user": "AND_USER_GROUP"
    }
]
with open('out.csv', 'w') as f:
    headers = list(data[0].keys())
    f.write(','.join(headers) + '\n')
    for entry in data:
        tmp = []
        for field in headers:
            tmp.append(entry[field])
        f.write(','.join(tmp) + '\n')

out.csv

depotFile,host,isgroup,line,perm,user
//ABND/JJEB/...,*,,16,open,5G_USER_GROUP
//LIB/...,*,,19,write,6G_USER_GROUP
//AND/RIO/...,*,,20,write,AND_USER_GROUP
Sign up to request clarification or add additional context in comments.

16 Comments

Did you try to run my code "as is" and got this error? Dont call the file csv - it is a module in python. Call it json_to_csv.py. You need to modify the code and load the json created by the shell script into data.
see how you load a json file into a list of dicts. see programiz.com/python-programming/json
added below whereas no repose, could you please help me with this. import json import pandas as pd with open('*.json') as json_file: ` data = json.load(json_file)` df = pd.DataFrame(data)
no need for pandas. just load the file into a list of dicts. see the example in the link
Now instead of output in txt changed to json ok will try with the link.
|

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.