0

In this python script i am making a REST call to a server and pulling information out in JSON files. With the following code, I am outputting 4 JSON files into a folder. I would like to output only 1 json file with all the information I need, how would I do this? If that's not possible then I would think merging the 4 JSON files would be my only option? Any help would be appreciated.

from subprocess import call
import os
import json
from glob import glob

fileName = "IP.txt"
file = open(fileName, "r")
for str in file:
login_info = str.split(':')
ip = login_info[0] 
username = login_info[1]
password = login_info[2]
os.mkdir(ip)

  call(["ilorest", "login", ip, '-u', 
  username, '-p', password])

  call(["ilorest", "save", '-- 
  selector=ComputerSystem', '--json', '-f', 
  ip+"\\"+ip+".json"])

  call(["ilorest", "save", '-- 
  selector=Memory', '--json', '-f', 
  ip+"\\"+ip+"_memory"+".json"])

  call(["ilorest", "save", '-- 
  selector=Processor', '--json', '-f', 
  ip+"\\"+ip+"_processor"+".json"])

  call(["ilorest", "save", '-- 
  selector=HPESmartStorageDiskDrive', '-- 
  json', '-f', 
  ip+"\\"+ip+"_drives"+".json"])

  call(["ilorest", "logout"])


paths = glob('*/')
for d in paths:
print(os.dir(d))
3
  • 1
    You can add all four elements to a list and just save the list as JSON. Look at subprocess.check_output Commented Aug 13, 2018 at 15:37
  • If merge were the solution, how would you do that? How shall the result look like? What do you want to have inside of it? Commented Aug 13, 2018 at 15:37
  • All the files have the exact same format, in fact, the first section of them are identical. The only reason i want them as a single file is because it will be easier for me to parse them and load them onto my sql table. I rather have 1 very long file for each IP than 4 files for each IP. Commented Aug 13, 2018 at 15:40

1 Answer 1

1

Just make a dict with all json objects, and dump it:

types = ['', '_memory', '_processor', '_drives']

result = {}
for suffix in types:
    with open(os.path.join(ip, ip + suffix + '.json')) as f:
        result[suffix.strip('_')] = json.load(f)
with open(os.path.join(ip, ip+ '_all.json'), 'w') as fw:
    json.dump(result, fw)
Sign up to request clarification or add additional context in comments.

3 Comments

Do i simply add that to the end of my file or should i edit my code somwhere?
@JohnMonte just add that. It will read all 4 files and generate a single file with the contents. That said, you might want to check the library from the other answer, because doing everything inside your python process seems more efficient than calling a subprocess 4 times. And then you could generate a single file in first place thus avoiding the merge.
Thats where I started to try to figure out if i can do a single process for all the information i needed. I will look at it again but i believe the calls have to be made separate, ill look again however because i agree its terrible inefficient. Thank you for your help!

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.