I'm having trouble modifying my code to add another dictionary to separate "hostNumber" and "hostMode" in my output. Below is the code that found here and manipulated:
import json
from json import dumps
top = "Top_Level"
top_dict = {}
top_dict["name"] = top
top_dict["sub_name"] = []
for site, site_data in df.groupby("site", sort=False):
site_dict = {}
site_dict["site"] = site
site_dict["sub_site"] = []
for stor, stor_data in site_data.groupby("system", sort=False):
stor_dict = {}
stor_dict["system"] = stor
stor_dict["sub_system"] = []
for port, port_data in stor_data.groupby("portId", sort=False):
port_dict = {}
port_dict["portId"] = port
port_dict["sub_portId"] = []
for host, host_data in port_data.groupby("hostName", sort=False):
host_data = host_data.drop(["portId", "system",
"site"], axis=1).set_index(
"hostName")
for n in host_data.to_dict(orient="records"):
port_dict["sub_portId"].append({"hostName": host,
"sub_hostName": [n]})
stor_dict["sub_system"].append(port_dict)
site_dict["sub_site"].append(stor_dict)
top_dict["sub_name"].append(site_dict)
top_out = dumps(top_dict)
parsed = json.loads(top_out)
resulting in:
print(json.dumps(parsed, indent=4, sort_keys=True))
{
"name": "Top_Level",
"sub_name": [
{
"site": "A",
"sub_site": [
{
"system": "system01",
"sub_system": [
{
"portId": "1-A",
"sub_portId": [
{
"hostName": "ahost005",
"sub_hostName": [
{
"hostNumber": "1",
"hostMode": "WIN"
}
]
}, ...
How can I modify my code to have it output in the following way:
...
"sub_hostName": [
{"hostNumber": "1"},
{"hostMode": "WIN"}
]...
Use the following line instead of "sub_hostName": [n]:
"sub_hostName": [dict([i]) for i in n.items()]
Related
I have text file and I want to convert it to JSON:
red|2022-09-29|03:15:00|info 1
blue|2022-09-29|10:50:00|
yellow|2022-09-29|07:15:00|info 2
so i type a script to convert this file into JSON:
import json
filename = 'input_file.txt'
dict1 = {}
fields =['name', 'date', 'time', 'info']
with open(filename) as fh:
l = 1
for line in fh:
description = list( line.strip().split("|", 4))
print(description)
sno ='name'+str(l)
i = 0
dict2 = {}
while i<len(fields):
dict2[fields[i]]= description[i]
i = i + 1
dict1[sno]= dict2
l = l + 1
out_file = open("json_file.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()
and output looks like this:
{
"name1": {
"name": "red",
"date": "2022-09-29",
"time": "03:15:00",
"info": "info 1"
},
"name2": {
"name": "blue",
"date": "2022-09-29",
"time": "10:50:00",
"info": ""
},
"name3": {
"name": "yellow",
"date": "2022-09-29",
"time": "07:15:00",
"info": "info 2"
}
}
As you can see I do so, but now I want to change looks of this JSON file. How can I change it to make my output looks like this:
to look like this:
[
{"name":"red", "date": "2022-09-29", "time": "03:15:00", "info":"info 1"},
{"name":"blue", "date": "2022-09-29", "time": "10:50:00", "info":""},
{"name":"yellow", "date": "2022-09-29", "time": "07:15:00", "info":"info 2"}
]
If you see your required json output, it is a list and not a dict like you have right now. So using a list(data) instead of dict(dict1) should give the correct output.
Following updated code should generate the json data in required format -
import json
filename = 'input_file.txt'
data = []
fields =['name', 'date', 'time', 'info']
with open(filename) as fh:
l = 1
for line in fh:
description = list( line.strip().split("|", 4))
print(description)
sno ='name'+str(l)
i = 0
dict2 = {}
while i<len(fields):
dict2[fields[i]]= description[i]
i = i + 1
data.append(dict2)
l = l + 1
out_file = open("json_file.json", "w")
json.dump(data, out_file, indent = 4)
out_file.close()
I would use pandas, it allows you to solve your problem in one statement and avoid reinventing a wheel:
import pandas as pd
pd.read_table("input_file.txt", sep="|", header=None,
names=["name", "date" , "time", "info"]).fillna("")\
.to_json("json_file.json", orient="records")
The dictionary I am trying to iterate through has the following structure:
d = {
"main_key_1": {
"name": "Name1",
"context": "Context1",
"message": "Message1",
"date": "Date1",
"reference": "Reference1"
},
"main_key_2": {
"name": "Name2",
"context": "Context2",
"message": "Message2",
"date": "Date2",
"reference": "Reference2"
}
}
This is the way I tried to iterate:
for item in d.items():
from_context = f"from {item[1]['context']}"
with context('given a descriptor'):
with context(from_context):
with before.all:
self.descriptor = item[1]['message']
with context('that contains a date'):
with it('recognizes the date'):
adapter = MessageToDatetAdapter(self.descriptor)
result = adapter.is_a_date()
expect(result).to(equal(True))
with it('extracts the date data'):
adapter = MessageToDatetAdapter(self.descriptor)
result = adapter.adapt()
expect(result['date']).to(equal(item[1]['date']))
expect(result['reference']).to(item[1]['reference'])
The first iteration would be something like below:
with context('given a descriptor'):
with context('from Context1'):
with before.all:
self.descriptor = 'Message1'
with context('that contains a date'):
with it('recognizes the date'):
adapter = MessageToDatetAdapter(self.descriptor)
result = adapter.is_a_date()
expect(result).to(equal(True))
with it('extracts the date data'):
adapter = MessageToDatetAdapter(self.descriptor)
result = adapter.adapt()
expect(result['date']).to('Date1')
expect(result['reference']).to('Reference1')
However, it seems like this is not correct. It looks like I cannot iterate through all the dictionary items.
I'm trying to print all values from a JSON file but it only prints the last one.
example Json file:
[
{
"folderTeste9": [
{
"_aliasinput": "folderTeste9",
"_idinput": "folderteste132131",
"_timesinput": [
"10:20"
],
"_statusinput": "true"
}
]
},
{
"testeFolder1991": [
{
"_aliasinput": "testeFolder1991",
"_idinput": "testefolder1991",
"_timesinput": [],
"_statusinput": "true"
}
]
},
{
"Flo.": [
{
"_aliasinput": "Flo.",
"_idinput": "12321354564613",
"_timesinput": [],
"_statusinput": "true"
}
]
}
]
My Code
import json
with open('config/'+'config.json', 'r') as file:
data: list = json.load(file)
lista = data
for element in lista:
print("")
for alias_element in element:
#print("Alias: " +alias_element)
for result in element[alias_element]:
profile_data = result
aliasInput = profile_data['_aliasinput']
timesInput = profile_data['_timesinput']
idInput = profile_data['_idinput']
statusInput = profile_data['_statusinput']
print(f" Values from register are {aliasInput}{timesInput}{idInput}{statusInput}")
Actual Result
Values from register are Flo. [] 12321354564613 true
Expected Result
I'd like to print all values from the different register.
Example:
Values from register are folderTest9 [10:20] folderteste132131 true
[...]
Values from register are Flo. [] 12321354564613 true
EDIT: used the suggested solution, now the json has more headers, as shown bellow.
I have input file as Json,
{
"systems-under-test": [{
"type": "url",
"sytems": [
"www.google.com",
"www.google.com",
"www.google.com"
]
},
{
"type": "api",
"sytems": [
"api.com",
"api.fr"
]
},
{
"type": "ip",
"sytems": [
"172.168 .1 .1",
"172.168 .1 .0"
]
}
],
"headers-configuration": [{
"custom": true,
"headers-custom-configuration": {
"headers": [{
"header-name": "x - frame - options",
"ignore": false,
"expected-value": ["deny", "sameorigin"]
},
{
"header-name": "content-security-policy",
"ignore": false,
"expected-value": []
}
]
}
}],
"header-results": []
}
after using the suggested solution by, I created dict that stores each header information and added these dict to a list.
for a single header in Json works fine, when I have multi headers its not working.
def load_header_settings2(self):
header_custom_setting = []
newDict = {}
path = self.validate_path()
with open(path) as json_file:
data = load(json_file)
config = data["headers-configuration"][0]
if config["custom"]:
headers = config["headers-custom-configuration"]["headers"]
headers_name = headers["header-name"]
ignore = headers["ignore"]
expected_values = headers["expected-value"]
newDict["header name"] = headers_name
newDict["ignore"] = ignore
newDict["expected value"] = expected_values
header_custom_setting.append(newDict)
newDict.clear()
for i in header_custom_setting:
print(i)
return header_custom_setting
can someone help?
This has been solved like this, is this safe way to reach my goal ?# RoadRunner
def load_header_settings2(self):
header_custom_setting = []
newDict = {}
path = self.validate_path()
with open(path) as json_file:
data = load(json_file)
config = data["headers-configuration"][0]
if config["custom"]:
headers = config["headers-custom-configuration"]["headers"]
for header in headers:
headers_name = header["header-name"]
ignore = header["ignore"]
expected_values = header["expected-value"]
newDict["header name"] = headers_name
newDict["ignore"] = ignore
newDict["expected value"] = expected_values
header_custom_setting.append(newDict)
for i in header_custom_setting:
print(i)
return header_custom_setting
You could get both header name and values like this, making sure we check "custom" is set to true before proceeding:
from json import load
with open("data.json") as json_file:
data = load(json_file)
config = data["headers-configuration"][0]
if config["custom"]:
headers = config["headers-custom-configuration"]["headers"]
headers_name = headers["header-name"]
print(headers_name)
expected_values = headers["expected-value"]
print(expected_values)
Output:
x - frame - options
['deny', 'sameorigin']
As for concatenating the headers and values, you could iterate the headers and values and combine them together into a string:
for value in expected_values:
print("%s %s" % (headers_name, value))
Or using f-strings:
for value in expected_values:
print(f"{headers_name} {value}")
Which will give you:
x - frame - options deny
x - frame - options sameorigin
The below query is grabbing data and creating a CSV file, the issue that I am having is that the source called ‘SPLE’ stores data in the database with numbers of 0, 1, 50.
However in the CSV those numbers are being collected in the CSV and I would like somehow when creating the CSV those number to represent words such as,
0 = True
1 = False
50 = Pending
Could someone show me how this is done please, I have been struggling on this?
My Code:
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST*"}}
]
}
}
}, size=10)
header_names = { 'DTDT': 'DATE', 'SPLE': 'TAG', ...}
with open('mycsvfile.csv', 'w') as f:
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names)
header_present = True
w.writerow(my_dict)
The output in the CSV file is:
Date SPLE Venue
20171016 1 Central
20171016 0 Central
20171016 50 Central
I'm assuming mycsvfile.csv file have SPLE column.
from elasticsearch import Elasticsearch
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST*"}}
]
}
}
}, size=10)
import pandas as pd
SPLE = {0:'true',1:'false',50:'pending'}
saved_csv = pd.read_csv('mycsvfile.csv',sep='\t')
saved_csv['SPLE'] = saved_csv['SPLE'].map(lambda x: SPLE[int(x)])
saved_csv.to_csv('edited_csv.csv', index=False)
Declare a dict somewhere for doing the translation:
SPLE_TRANSLATION = {0: 'True', 1: 'False', 50: 'Pending'}
Then, inside your loop:
my_dict['SPLE'] = SPLE_TRANSLATION[my_dict['SPLE']]
w.writerow(my_dict)