I'm new to Python programming, so do bear with me if I make any mistakes anywhere
I'm trying to write a json file using 2 dictionaries and dump the output to the file using the following code on Windows
import json
import sys
import string
from time import strftime
scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
for line in scan_results:
if ".jpg" in line:
lst = []
result = line.split('\\')
result_split = result[5].split(' ')
filename = result_split[0]
raw_status = result_split[3]
if "OK" in raw_status:
status = "Okay"
status_code = "0"
dict = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status}
dict2 = {filename : dict}
print json.dumps(dict2)
sys.stdout = saved
f.close()
print "JSON report written"
The problem is, the output that I have is
{
"car-30537.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30537.jpg"
}
}{
"car-30538.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30538.jpg"
}
}
whereas the output that I want is
{
"car-30537.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30537.jpg"
},
{
"car-30538.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30538.jpg"
}
}
Is there any ways to correct this problem? Thanks in advance
You are making lots of dicts, while you only need one main containing one:
import json
import sys
import string
from time import strftime
scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
dict2 = {} #Create one output dict
for line in scan_results:
if ".jpg" in line:
lst = []
result = line.split('\\')
result_split = result[5].split(' ')
filename = result_split[0]
raw_status = result_split[3]
if "OK" in raw_status:
status = "Okay"
status_code = "0"
dict2[filename] = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status} #Add to that dict.
print json.dumps(dict2) #Print it out at the end.
sys.stdout = saved
f.close()
print "JSON report written"
I added comments to modified lines.
Related
I have json file, i need to rename folder_path key to backup_folder_path using python:
{
"urn:adsk.wipprod:dm.lineage:smth": {
"bim_manifest_urn": "urn:foo/bar/z",
"gs_id": "foobar",
"versions": {
"1": "1"
},
"folder_path": "/foo/bar"
},
"urn:adsk.wipprod:dm.lineage:smth": {
"bim_manifest_urn": "urn:foo/bar",
"gs_id": "foobar1",
"versions": {
"1": "1"
},
"folder_path": "/foo/barŠ”"
},
What I tried to do:
def edit_string_name():
with open(r"smth.json", encoding="utf-8") as json_data:
data = json.load(json_data)
data = {'folder_path'}
data['backup_folder_path'] = data.pop('folder_path')
print(data)
if __name__ == '__main__':
edit_string_name()
But nothing seems to happen.
When I tried to cycle through I got nonsense in terminal.
This should do the job
def edit_string_name():
with open("smth.json", "r+", encoding="utf-8") as file:
data = json.load(file)
content = data["urn:adsk.wipprod:dm.lineage:smth"]
content["backup_folder_path"] = content["folder_path"]
content.pop("folder_path")
data["urn:adsk.wipprod:dm.lineage:smth"] = content
# Updating the file
file.seek(0)
file.write(json.dumps(data, indent=4))
file.truncate()
edit_string_name()
I wrote a code in python that converts a file with these objects to JSON. It converts into the proper json format but the output is not exactly what I need.
{
name: (sindey, crosby)
game: "Hockey"
type: athlete
},
{
name: (wayne, gretzky)
game: "Ice Hockey"
type: athlete
}
Code:
import json
f = open("log.file", "r")
content = f.read()
splitcontent = content.splitlines()
d = []
for line in splitcontent:
appendage = {}
if ('}' in line) or ('{' in line):
# Append a just-created record and start a new one
continue
d.append(appendage)
key, val = line.split(':')
if val.endswith(','):
# strip a trailing comma
val = val[:-1]
appendage[key] = val
with open("json_log.json", 'w') as file:
file.write((json.dumps(d, indent=4, sort_keys=False)))
Desired output:
[
{
"name": "(sindey, crosby)",
"game": "Hockey",
"type": "athlete"
},
{
"name": "(wayne, gretzky)",
"game": "Ice Hockey",
"type": "athlete"
}
]
But I'm getting:
[
{
" name": " (sindey, crosby)"
},
{
" game": " \"Hockey\""
},
{
" type": " athlete"
},
{
" name": " (wayne, gretzky)"
},
{
" game": " \"Ice Hockey\""
},
{
" type": " athlete"
}
]
Any way to fix it to get the desired output and fix the {} around each individual line?
It's usually a good idea to split parsing into simpler tasks, e.g. first parse records, then parse fields.
I'm skipping the file handling and using a text variable:
intxt = """
{
name: (sindey, crosby)
game: "Hockey"
type: athlete
},
{
name: (wayne, gretzky)
game: "Ice Hockey"
type: athlete
}
"""
Then create a function that can yield all lines that are part of a record:
import json
def parse_records(txt):
reclines = []
for line in txt.split('\n'):
if ':' not in line:
if reclines:
yield reclines
reclines = []
else:
reclines.append(line)
and a function that takes those lines and parses each key/value pair:
def parse_fields(reclines):
res = {}
for line in reclines:
key, val = line.strip().rstrip(',').split(':', 1)
res[key.strip()] = val.strip()
return res
the main function becomes trivial:
res = []
for rec in parse_records(intxt):
res.append(parse_fields(rec))
print(json.dumps(res, indent=4))
the output, as desired:
[
{
"name": "(sindey, crosby)",
"game": "\"Hockey\"",
"type": "athlete"
},
{
"name": "(wayne, gretzky)",
"game": "\"Ice Hockey\"",
"type": "athlete"
}
]
The parsing functions can of course be made better, but you get the idea.
Yes I haven't checked the ouput properly, I remodified the logic now. The output is as expected.
import json
f = open("log.file", "r")
content = f.read()
print(content)
splitcontent = content.splitlines()
d = []
for line in splitcontent:
if "{" in line:
appendage = {}
elif "}" in line:
d.append(appendage)
else:
key, val = line.split(':')
appendage[key.strip()] = val.strip()
with open("json_log.json", 'w') as file:
file.write((json.dumps(d, indent=4, sort_keys=False)))
I searched for a long time but I am not really familiar with python and json and I can't find the answer of my problem.
Here is my Python script
import json
jsonFile = open("config.json", "r")
data = json.load(jsonFile)
data.format(friendly, teaching, leader, target)
print(data)
Here is json the file:
{
"commend": {
"friendly": {},
"teaching": {},
"leader": {}
},
"account": {
"username": "",
"password": "",
"sharedSecret": ""
},
"proxy": {
"enabled": false,
"file": "proxies.txt",
"switchProxyEveryXaccounts": 5
},
"type": "COMMEND",
"method": "SERVER",
"target": "https://steamcommunity.com/id/{}",
"perChunk": 20,
"betweenChunks": 300000,
"cooldown": 28800000,
"steamWebAPIKey": "{}",
"disableUpdateCheck": false
}
I tried .format but we can't use this method with with a dictionary.
With your help I managed to find the answer A big thank you for your speed and your help ! Here is what I did:
import json
jsonFile = open("config.json", "r")
data = json.load(jsonFile)
(data['commend']['friendly']) = nbfriendly
(data['commend']['teaching']) = nbteaching
(data['commend']['leader']) = nbleader
print(data)
print(data)
A json file is a dictionary, so you can use dict methods with it. Here is the code:
import json
with open("config.json", "r") as json_file:
data = json.load(json_file)
# Let's say you want to add the string "Hello, World!" to the "password" key
data["account"]["password"] += "Hello, World!"
# Or you can use this way to overwrite anything already written inside the key
data["account"]["password"] = "Hello, World!"
print(data)
You can add data by tranversing through it like a dictionary:
data['key'] = value
Example:
dic["commend"]["friendly"]={'a':1}
I am trying to update a new key into my JSON file if the conditions are met. The following is my python code attempting to make multiple updates in a JSON file.
#!/usr/bin/env python
# Usage: update json file
import json
import os
json_dir="/opt/rdm/adggeth/ADGG-ETH-02/20181008/"
json_dir_processed="/opt/rdm/adggeth/ADGG-ETH-02/20181008updated/"
for json_file in os.listdir(json_dir):
if json_file.endswith(".json"):
processed_json = "%s%s" % (json_dir_processed, json_file)
json_file = json_dir + json_file
print "Processing %s -> %s" % (json_file, processed_json)
with open(json_file, 'r') as f:
json_data = json.load(f)
# replacement mapping
update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
"grp_farmerdts/hh_region":"grp_farmerdts/region",
"grp_farmerdts/hh_district":"grp_farmerdts/district",
"grp_farmerdts/hh_ward":"grp_farmerdts/ward",
"grp_farmerdts/hh_village":"grp_farmerdts/village"}
diff_keys = update_map.keys() - json_data.keys()
if not diff_keys:
print("No Update to JSON keys")
else:
for k in diff_keys:
json_data[k] = json_data[update_map[k]]
with open(processed_json, 'w') as f:
f.write(json.dumps(json_data, indent=4))
else:
print "%s not a JSON file" % json_file
The JSON file i am trying to make update to is as follows:
{
....
"farmerregistrd": "1",
"grp_farmerdts/region": "5",
"datacollid": "0923678275",
"_status": "submitted_via_web",
"enumtype": "2",
"deviceid": "352948096845916",
"start_time": "2019-04-03T10:57:23.620+03",
"_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
"grp_farmerdts/village": "2852",
"_submitted_by": null,
"formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
"grp_farmerdts/hh_id": "623",
"grp_farmerdts/ward": "136",
...
"_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
"_id": 711097,
"grp_farmerdts/district": "31"
}
My expected output from running the following python file is as follows
{
....
"farmerregistrd": "1",
"grp_farmerdts/hh_region": "5",
"datacollid": "0923678275",
"_status": "submitted_via_web",
"enumtype": "2",
"deviceid": "352948096845916",
"start_time": "2019-04-03T10:57:23.620+03",
"_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
"grp_farmerdts/hh_village": "2852",
"_submitted_by": null,
"formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
"grp_farmerdts/hh_id": "623",
"grp_farmerdts/hh_ward": "136",
...
"_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
"_id": 711097,
"grp_farmerdts/hh_district": "31"
}
Using re module and json.loads() with object_hook= parameter (doc). This script will add hh_ prefix to every grp_farmerdts/* key where isn't:
json_str = '''{
"farmerregistrd": "1",
"grp_farmerdts/region": "5",
"datacollid": "0923678275",
"_status": "submitted_via_web",
"enumtype": "2",
"deviceid": "352948096845916",
"start_time": "2019-04-03T10:57:23.620+03",
"_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
"grp_farmerdts/village": "2852",
"_submitted_by": null,
"formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
"grp_farmerdts/hh_id": "623",
"grp_farmerdts/ward": "136",
"_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
"_id": 711097,
"grp_farmerdts/district": "31"
}'''
import re
import json
def change_keys(d):
return {re.sub(r'grp_farmerdts/((?!hh_)(\w+))', r'grp_farmerdts/hh_\1', k): v for k, v in d.items()}
print(json.dumps(json.loads(json_str, object_hook=change_keys), indent=4))
Prints:
{
"farmerregistrd": "1",
"grp_farmerdts/hh_region": "5",
"datacollid": "0923678275",
"_status": "submitted_via_web",
"enumtype": "2",
"deviceid": "352948096845916",
"start_time": "2019-04-03T10:57:23.620+03",
"_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
"grp_farmerdts/hh_village": "2852",
"_submitted_by": null,
"formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
"grp_farmerdts/hh_id": "623",
"grp_farmerdts/hh_ward": "136",
"_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
"_id": 711097,
"grp_farmerdts/hh_district": "31"
}
According to your expected output all particular keys need to be checked (not one of them). Change your logic as shown below:
...
json_data = json.load(f)
# replacement mapping
update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
"grp_farmerdts/hh_region":"grp_farmerdts/region",
"grp_farmerdts/hh_district":"grp_farmerdts/district",
"grp_farmerdts/hh_ward":"grp_farmerdts/ward", "grp_farmerdts/hh_village":"grp_farmerdts/village"}
diff_keys = update_map.keys() - json_data.keys()
if not diff_keys:
print("No Update to JSON keys")
else:
for k in diff_keys:
if update_map[k] in json_data:
json_data[k] = json_data[update_map[k]]
I am trying to search for a variable in a JSON file.
Current JSON file (devices.json) is:
{
"NYC": {
"Floor1": [
{
"name": "server1",
"ip": "1.1.1.1"
},
{
"name": "server2",
"ip": "1.1.1.2"
}
],
"Floor2": [
...
],
"sitenum": 1
},
"Boston": {
...
"sitenum": 2
}
...
}
Two questions:
I am new to JSON, so is this formatted correctly for
lists/dictionaries?
I'd like to preform a python query to display Floor(s){name, ip} for sitenum (x)
Current Python file is
import json
with open('devices.json') as jsonfile:
data = json.load(jsonfile)
Thanks!
this python script will return the floor details as a list of json values for a given sitenum and floor.
import json
def get_floor_details(sitenum, floor, data):
for k,v in data.items():
if v['sitenum'] == sitenum:
return(v[floor])
with open('sample.json') as json_file:
data = json.load(json_file)
sitenum = 1
floor = 'Floor1'
floor_details = get_floor_details(sitenum, floor, data)
print(floor_details)
Output:
[{'name': 'server1', 'ip': '1.1.1.1'}, {'name': 'server2', 'ip': '1.1.1.2'}]
def findFloor(site_num_val, data):
return_val = {}
for each_loc in data:
if site_num_val == each_loc["sitenum"]:
return_val = data[each_loc].copy()
del return_val["sitenum"]
break
else:
print "sitenum not found"
return return_val
I hope this solves your problem in trying to get the information.