I've a json file in the following format
{
"project1": {
"alias1": {
"command":"somecommand for alias1"
}
},
"project2": {
"alias2": {
"command":"somecommand for alias2"
},
"alias3": {
"command":"somecommand for alias3"
}
}
I want to extract the data and print the output as a Dict in the following format
{
'alias1':'command', # Command for alias1
'alias2':'command', # Command for alias2
'alias3':'command' # Command for alias3
}
How can I achieve this in python2 ?
Firstly there's an error in your source JSON, it needs a closing "}"
After fixing that you can use json.loads to get it into a Python variable, and then run over it with Python loops and conditionals to extract what you want. Something like
ss = json.loads( """ # JSON source as above
# etc
""")
res = {}
for proj,v in ss.items():
for alias,d in v.items():
if alias.startswith("alias"):
res[alias] = d["command"]
res is the dict you want, and you can print it using json.dumps and (probably) indent=0 and sort_keys=True
Not sure if this is what you were looking for:
result = {value:v for key in a for value in a[key] for item in a[key][value] for k,v in a[key][value].items()}
{'alias2': 'somecommand for alias2',
'alias3': 'somecommand for alias3',
'alias1': 'somecommand for alias1'}
you can try like this
def dict_parse(dict):
dict_new = {}
keys = dict.keys()
for k in keys:
key_new = dict[k].keys()
for i in key_new:
dict_new[i] = dict[k][i]
return dict_new
output will be this :
{'alias1': {'command': 'somecommand for alias1'},
'alias2': {'command': 'somecommand for alias2'},
'alias3': {'command': 'somecommand for alias3'}}
i think you should try this based on this you can get your exact output
you can use this
print({i:j['command'] for k,v in df.items() for i,j in v.items() if i.startswith('alias')})
Related
I am trying to store two run options in Json. My attempt is as below. Is this a sensible way to organise Json object? Also how can I read either of them into tuples? I tried tuple([(item['opt_one'],item['val_one']) for item in config['opts']]) but git a key error. Many thanks for your help.
"opts":[
{
"opt_one": "one_option",
"val_one" : "value_one"
},
{
"opt_two": "two_option",
"val_two" : "value_two"
}
]
# or it can be done in 1 line
print(tuple(map(lambda v: tuple(v.values()), J['opts'])))
import json
j = """
{"opts":[
{
"opt_one": "one_option",
"val_one" : "value_one"
},
{
"opt_two": "two_option",
"val_two" : "value_two"
}
]
}
"""
# note the braces around "opts"
J = json.loads(j) #turns it into a python object
print(J) #see!
# the long way to do it, but you can see the process
R = []
for item in J['opts']:
r = []
for v in item.values():
r.append(v)
R.append(tuple(r))
print(tuple(R)) # solution
I am trying to get a JSON sub-schema's "name" from based off of its contents. This is kind of hard to explain, so an example would be better:
{
"dummy_name_1": {
"dummy_key_1": "unique_dummy_value_1",
"dummy_key_2": "dummy_value_2"
},
"dummy_name_2": {
"dummy_key_1": "unique_dummy_value_2",
"dummy_key_2": "dummy_value_2"
}
}
I want to get the name of dummy_name_1 (which would be "dummy_name_1") given the value of the key "dummy_key_1" (which would be "unique_dummy_value_1"). Basically, if I give the Python function I want "dummy_key_1" and "unique_dummy_value_1" as parameters, I want it to return the string "dummy_name_1".
Something like this? structure being your dict.
def get_dummy_name(dummy_key, dummy_value):
for dummy_name, content in structure.items():
if dummy_key in content.keys() and content[dummy_key] == dummy_value:
return dummy_name
try with this:
def get_category_name(key_name, key_value):
dictionary = {
"dummy_name_1": {
"dummy_key_1": "unique_dummy_value_1",
"dummy_key_2": "dummy_value_2"
},
"dummy_name_2": {
"dummy_key_1": "unique_dummy_value_2",
"dummy_key_2": "dummy_value_2"
}
}
for elem in dictionary.items():
if key_name in elem[1] and elem[1][key_name] == key_value:
return elem[0]
return False
response = get_category_name('dummy_key_1', 'unique_dummy_value_1')
I have created the below logic to add list to a dictionary in Python it works fine
>>> dict ={}
>>> dict ={"key":[]}
>>> dict['key']
[]
>>> dict['key'].append('a')
>>> dict['key']
['a']
When I try to Implement same thing while parsing the JSON and creating dynamic dictionary based on certain it failed to add in the List and report as NoneType
import json
json_data="""{
"data":[
{
"package_path":"/bin/bash",
"package_type":"rpm"
},
{
"package_path":"com.test3",
"package_type":"java"
},
{
"package_path":"com.test",
"package_type":"java"
}
]
}
"""
j_dict = json.loads(json_data)
dict2 = {}
for vuln in j_dict['data']:
package_type = vuln['package_type']
package_path = vuln['package_path']
if package_type in dict2:
dict2 ={package_type:[]}
else:
dict2[package_type].append(package_path)
Throws error
% python ~/Desktop/test.py
Traceback (most recent call last):
File "test.py", line 30, in <module>
dict2[package_type].append(package_path)
KeyError: u'rpm'
Expecting output like
dict2 {"java":['com.test3','com.test2'],"rpm":['/bin/bash']}
You can use dict.setdefault to create empty list if the key is not found inside the dictionary. For example:
import json
json_data="""{
"data":[
{
"package_path":"/bin/bash",
"package_type":"rpm"
},
{
"package_path":"com.test3",
"package_type":"java"
},
{
"package_path":"com.test",
"package_type":"java"
}
]
}
"""
j_dict = json.loads(json_data)
out = {}
for vuln in j_dict['data']:
out.setdefault(vuln['package_type'], []).append(vuln['package_path'])
print(out)
Prints:
{'rpm': ['/bin/bash'], 'java': ['com.test3', 'com.test']}
defaultdict can be used here.
import json
from collections import defaultdict
json_data = """{
"data":[
{
"package_path":"/bin/bash",
"package_type":"rpm"
},
{
"package_path":"com.test3",
"package_type":"java"
},
{
"package_path":"com.test",
"package_type":"java"
}
]
}
"""
j_dict = json.loads(json_data)
data = defaultdict(list)
for entry in j_dict['data']:
data[entry['package_type']].append(entry['package_path'])
print(data)
output
defaultdict(<class 'list'>, {'rpm': ['/bin/bash'], 'java': ['com.test3', 'com.test']})
You have your if statement slightly wrong:
if package_type in dict2:
dict2 ={package_type:[]}
else:
dict2[package_type].append(package_path)
Should be:
if package_type not in dict2:
dict2[package_type] = []
dict2[package_type].append(package_path)
You were saying, if a key was in the dictionary, replace the whole dictionary with a single key and a list as the value.
In the else clause you were saying, if the key doesn't exist, fetch the value for that key, assume it's a list, and append to it. Which fails.
This idiom of either adding value if it doesn't exist or using the existing value is so common, there are a couple of different ways of doing it built into python and its libraries.
dict.setdefault will either set a new default value for a key, or return the existing value if it exists. I find its call syntax ugly:
dict2.setdefault(package_type, []).append(package_path)
This sets the key's value to [] if it doesn't exist, returns it, and then you can append to the list as it exists in the dictionary.
An alternative that I prefer is to use collections.defaultdict, which is a dictionary that automatically creates a default value when a key doesn't already exist:
dict2 = defaultdict(list)
dict2[package_type].append(package_path)
I'm receiving json files improperly and am trying to create a temporary fix until the documents come in the proper format. Instead of the value being set to the derivationsValue key, it is being set as a key value pair, so there is an extraneous key. I want to set the the inner value to the outer key.
{
"derivationName": "other_lob_subcd",
"derivationValue": {
"OOP3": "TENANT"
}
}
Given the above json, I want the result to be
{
"derivationName": "other_lob_subcd",
"derivationValue": "TENANT"
}
I could also live with
{
"derivationName": "other_lob_subcd",
"derivationValue": "OOP3-TENANT"
}
or something like that. It just can't be another json element.
Based on #Diana Ayala's answer, I have written this to try solving the problem with variable keys.
for k,v in data['mcsResults']['derivationsOutput']:
if isinstance(k['derivationValue'], dict):
for sk, sv in k['derivationValue']:
k['derivationValue'] = sv
You can use below generic code for your requirement.
import json
filePath = 'file.json'
def getValue(value):
if type(value) is dict:
ans = list(value)[0]
for k in value:
ans += '-'+getValue(value[k])
return ans
return value
def correct(data):
for key in data:
data[key] = getValue(data[key])
return data
if __name__ == "__main__":
with open(filePath) as fp:
data = json.load(fp)
data = correct(data)
print (data)
output:
D:\>python file.py
{'derivationName': 'other_lob_subcd', 'derivationValue': 'OOP3-TENANT'}
For the example given:
import json
with open('inpt.txt') as json_file:
data = json.load(json_file)
data['derivationValue'] = data['derivationValue']['OOP3']
Gives the output:
{'derivationName': 'other_lob_subcd', 'derivationValue': 'TENANT'}
In general, you can look at the solutions here.
You can do something like this:
val = {
"derivationName": "other_lob_subcd",
"derivationValue": {
"OOP3": "TENANT"
}
}
val["derivationValue"] = val["derivationValue"]["OOP3"]
print(val)
This will be the output:
val = {
"derivationName": "other_lob_subcd",
"derivationValue": "TENANT"
}
How do I convert delimited strings into a hierarchical JSON in Python? (I saw a solution for a similar question in jQuery. I have been trying to find a Python solution for the same.)
The goal is to generate a hierarchical JSON of categories from a bunch of URLs which might look like:
sport/tennis/grandslams
sport/chess
sport/chess/players/men
sport/tennis
sport/cricket/stadiums
sport/tennis/players
You could achieve this with dictionnaries :
initial = ["Fred-Jim","Fred","Fred-Jim-Bob", "Fred-Jim-Jack", "John", "John-Jim"]
result = {}
for item in initial:
hierarchy = item.split('-')
local_result = result
for node in hierarchy:
local_result = local_result.setdefault(node, {})
print result
Will give you :
{
'John': {
'Jim': {}
},
'Fred': {
'Jim': {
'Bob': {},
'Jack': {}
}
}
}
Are you looking for json.dumps() from the json module?
edit:
oh, ok, I get you now. Maybe something like:
#paths is a list of strings containing the paths you are scraping from your site.
hierarchy = {}
for path in paths:
cursor = hierarchy
for part in path.split('/'):
if part not in cursor:
cursor[part] = {}
cursor = cursor[part]