my_json file has list of nested dicts, I need to print only the username if type==Developer-Verified and it's value==1, I managed to print just the approvals list, unable to to go further.
$ cat myjson_file | python3.6 -c "import sys, json; approvals=json.load(sys.stdin)['currentPatchSet']['approvals']; print(json.dumps(approvals, indent=4))"
[
{
"type": "Developer-Verified",
"description": "Developer-Verified",
"value": "1",
"grantedOn": 1581451370,
"by": {
"name": "Donald Snifer",
"email": "dsnifer#gmail.com",
"username": "dsnifer"
}
},
{
"type": "Code-Review",
"description": "Code-Review",
"value": "2",
"grantedOn": 1581623684,
"by": {
"name": "Brandon Welch",
"email": "bwelch#gmail.com",
"username": "bwelch"
}
},
{
"type": "Developer-Verified",
"description": "Developer-Verified",
"value": "1",
"grantedOn": 1581451370,
"by": {
"name": "Hamlin Damer",
"email": "hdamer#gmail.com",
"username": "hdamer"
}
}
]
$
I need to print just dsnifer hdamer
I tried to move further with below and other logics, and I keep failing
python3.6 -c "import sys, json; approvals=json.load(sys.stdin)['currentPatchSet']['approvals']; print( k for k,v in approvals[0].items())"
Loop inside json and check if type and value meet's the condition if so just print the username.
for ele in jsonData :
if ele['type'] == 'Developer-Verified' and ele['value'] == '1' :
print(ele["by"]["username"], end=" ")
Related
Trying to update JSON attribute value in line using JPATH.
Trying a solution in Python but also assessing Snowpark alternative(assuming data loaded in a table in variant column).
Python code is working for Objects but failing in case of arrays involved.
Python code:
json={
"ID": "1",
"NAME": { "FIRST_NAME": "ABC", "LAST_NAME": "XYZ" },
"ADDR": [
{ "TYPE": "HOME", "ADDR_L1": "SDGSG", "CITY": "AFAFA" },
{ "TYPE": "OFFC", "ADDR_L1": "AFASF", "CITY": "SDGSDG" }
],
"CONTACT": { "CONTACTS": [{ "TYPE": "A" }, { "TYPE": "B" }, { "TYPE": "C" }] },
"LEVEL1OBJ": {
"LEVEL2ARR": [{ "LEVEL3OBJ": "A" }, { "LEVEL3OBJ": "B" }],
"LEVEL2ARR_1":[{"LEVEL3ARR":[{"LEVEL4OBJ":"A"},{"LEVEL4OBJ":"B"}]},{"LEVEL3ARR":[{"LEVEL4OBJ":"C"},{"LEVEL4OBJ":"D"}]}],
"LEVEL2OBJ": "GFDB"
}
}
#Below input works
#keys=['NAME','FIRST_NAME']
#Below doesnt work
#keys=['ADDR','0','ADDR_L1']
#keys=['CONTACT','CONTACTS','0','TYPE']
keys=['LEVEL1OBJ','LEVEL2ARR_1','0','LEVEL3ARR','0','LEVEL4OBJ']
from functools import reduce
import operator
def get_by_path(root, items):
return reduce(operator.getitem, items, root)
def set_by_path(root, items, value):
get_by_path(root, items[:-1])[items[-1]] = value
set_by_path(json,keys,'')
print(json)
Does anyone have had experience with this?
What could be the code in Snowpark?
I have a python dictionary that looks something like this:
[
{
"timestamp": 1621559698154,
"user": {
"uri": "spotify:user:xxxxxxxxxxxxxxxxxxxx",
"name": "Panda",
"imageUrl": "https://i.scdn.co/image/ab67757000003b82b54c68ed19f1047912529ef4"
},
"track": {
"uri": "spotify:track:6SJSOont5dooK2IXQoolNQ",
"name": "Dirty",
"imageUrl": "http://i.scdn.co/image/ab67616d0000b273a36e3d46e406deebdd5eafb0",
"album": {
"uri": "spotify:album:0NMpswZbEcswI3OIe6ml3Y",
"name": "Dirty (Live)"
},
"artist": {
"uri": "spotify:artist:4ZgQDCtRqZlhLswVS6MHN4",
"name": "grandson"
},
"context": {
"uri": "spotify:artist:4ZgQDCtRqZlhLswVS6MHN4",
"name": "grandson",
"index": 0
}
}
},
{
"timestamp": 1621816159299,
"user": {
"uri": "spotify:user:xxxxxxxxxxxxxxxxxxxxxxxx",
"name": "maja",
"imageUrl": "https://i.scdn.co/image/ab67757000003b8286459151d5426f5a9e77cfee"
},
"track": {
"uri": "spotify:track:172rW45GEnGoJUuWfm1drt",
"name": "Your Best American Girl",
"imageUrl": "http://i.scdn.co/image/ab67616d0000b27351630f0f26aff5bbf9e10835",
"album": {
"uri": "spotify:album:16i5KnBjWgUtwOO7sVMnJB",
"name": "Puberty 2"
},
"artist": {
"uri": "spotify:artist:2uYWxilOVlUdk4oV9DvwqK",
"name": "Mitski"
},
"context": {
"uri": "spotify:playlist:0tol7yRYYfiPJ17BuJQKu2",
"name": "I Bet on Losing Dogs",
"index": 0
}
}
}
]
How can I get, for example, the group of values for user.name "Panda" and then get that specific "track" list? I can't parse through the list by index because the list order changes randomly.
If you are only looking for "Panda", then you can just loop over the list, check whether the name is "Panda", and then retrieve the track list accordingly.
Otherwise, that would be inefficient if you want to do that for many different users. I would first make a dict that maps user to its index in the list, and then use that for each user (I am assuming that the list does not get modified while you execute the code, although it can be modified between executions.)
user_to_id = {data[i]['user']['name']: i for i in range(len(data))} # {'Panda': 0, 'maja': 1}
def get_track(user):
return data[user_to_id[user]]['track']
print(get_track('maja'))
print(get_track('Panda'))
where data is the list you provided.
Or, perhaps just make a dictionary of tracks directly:
tracks = {item['user']['name']: item['track'] for item in data}
print(tracks['Panda'])
If you want to get list of tracks for user Panda:
tracks = [entry['track'] for entry in data if entry['user']['name'] == 'Panda']
Giving the parameters of Interface=['1/1/1', '1/2/3'], tag=11, I need to add the missing interface to json_data[Interface] and json_data[Port] as below. All places except name value used '%2F' as '/'.
Input json:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
}
}
}
Output json should be look like this:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
},
"1%2F2%2F3": {
"name": "1/2/3",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
},
"1%2F2%2F3": {
"interfaces": [
"1%2F2%2F3"
],
"name": "1/2/3",
"tag": "11"
}
}
}
I tried with iterate interface list:
for item in interface:
if item is not in json_data["Interface"].keys():
json_data["Interface"][item] = { "name" : item }
But returned in wrong format:
"Interface": {
"2": {
"name": "2"
},
"1": {
"name": "1"
},
"'": {
"name": "'"
},
" ": {
"name": " "
},
",": {
"name": ","
},
"/": {
"name": "/"
},
How to deal with the slash and %2F
Here's a simple way to convert from / to %2F:
'1/1/1'.replace('/', '%2F')
# Results: '1%2F1%2F1'
I'm not sure why you need it in that format, but if it's related to passing it through a URL and you need to escape other characters, you could use:
import urllib
urllib.parse.quote('1/1/1', safe='')
# Results: '1%2F1%2F1'
Your output
It looks like you're accidentally iterating over a string of letters and not a list of interfaces. How are you assigning the interface variable?
Your check for existing interfaces
Your condition if item is not in json_data["Interface"].keys() will not work if item is directly from your input in the format 1/1/1 because all of the keys in json_data["Interface"] use the escaped format.
I would like to print the value of every occurance in a JSON string with Python.
Here is my JSON:
{
"changed": false,
"results": [{
"arch": "x86_64",
"epoch": "0",
"name": "nagios-plugins-check_ansible",
"nevra": "0:nagios-plugins-check_ansible-20170803-4.1.x86_64",
"release": "4.1",
"repo": "nagios_plugins",
"version": "20170803",
"yumstate": "available"
},
{
"arch": "x86_64",
"epoch": "0",
"name": "nagios-plugins-check_memory",
"nevra": "0:nagios-plugins-check_memory-20170801-19.1.x86_64",
"release": "19.1",
"repo": "nagios_plugins",
"version": "20170801",
"yumstate": "available"
},
{
"arch": "x86_64",
"epoch": "0",
"name": "nagios-plugins-check_radius",
"nevra": "0:nagios-plugins-check_radius-20170802-3.1.x86_64",
"release": "3.1",
"repo": "nagios_plugins",
"version": "20170802",
"yumstate": "available"
}
]
}
I would like to print every occurence of the "nevra" key to the console. I have tried:
import json, sys
obj=json.load(sys.stdin)
i = 0
while True:
try:
print(obj["results"][i]["nevra"])
i = (i + 1)
except IndexError:
exit(0)
but this produces:
NameError: name 'false' is not defined
Simply use:
for result in obj['results']:
print(result['nevra'])
This generates:
>>> for result in obj['results']:
... print(result['nevra'])
...
0:nagios-plugins-check_ansible-20170803-4.1.x86_64
0:nagios-plugins-check_memory-20170801-19.1.x86_64
0:nagios-plugins-check_radius-20170802-3.1.x86_64
You can finish it one line also :
nevra = [ v["nevra"] for v in data['results']]
Output :
['0:nagios-plugins-check_ansible-20170803-4.1.x86_64', '0:nagios-plugins-check_memory-20170801-19.1.x86_64', '0:nagios-plugins-check_radius-20170802-3.1.x86_64']
to print every occurence of the "nevra" key to the console
Discover jq tool:
jq -r '.results[] | if has("nevra") then .nevra else empty end' yourjsonfile
The output:
0:nagios-plugins-check_ansible-20170803-4.1.x86_64
0:nagios-plugins-check_memory-20170801-19.1.x86_64
0:nagios-plugins-check_radius-20170802-3.1.x86_64
i get many json strings from a mysql DB an should combine them.
For example:
{
"type": "device",
"name": "Lampe",
"controls": [
{
"type": "switch",
"name": "Betrieb",
"topic": "/lampe/schalter"
}
]
}
in combination this devices should get into a array of a json file
{
"name": "Test-System",
"devices": [
{
"type": "device",
"name": "Lampe",
"controls": [
{
"type": "switch",
"name": "Betrieb",
"topic": "/lampe/schalter"
}
]
},
{
other Device
}
]
}
i do not understand how to do this in python
does someone have a idea how to do it ?
The json module can be used.
#!/usr/bin/env python3.5
import json
# Parse each device JSON file.
device1 = json.load(open("device-switch-Lampe.json"))
device2 = json.load(open("device-sensor-Wert.json"))
# more devices ...
obj = {"name": "Test-System", "devices": [device1, device2]}
print(json.dumps(obj))
Output (prettified):
{
"devices": [{
"type": "device",
"controls": [{
"type": "switch",
"topic": "/lampe/schalter",
"name": "Betrieb"
}],
"name": "Lampe"
}, {
"type": "device",
"controls": [{
"type": "sensor",
"topic": "/sensor/wert",
"name": "Wert"
}],
"name": "Sensor"
}],
"name": "Test-System"
}
There are two ways you could do this - by working on strings, or by working with Python-JSON data structures. The former would be something like
# untested code
s = '''{
"name": "Test-System",
"devices": [ '''
while True:
j = get_json_from_DB()
if not j: break # null string or None
s = s + j + ',\n'
s = s[:-2] + ']\n}\n' #[:-2 loses the last ',\n' from the loop
Or if you want to work with Python loaded-JSON then
import json
# untested code
s = {
"name": "Test-System",
"devices": []
}
while True:
j = get_json_from_DB()
if not j: break # null string or None
s['devices'].append( json.loads(j) )
# str = json.dumps(s) # ought to be valid
This latter will validate all your incoming json-strings (json.loads() will throw an exception for any bad JSON) and will be more efficient for large numbers of devices. It's therefore to be preferred unless you are working in a RAM-constrained embedded system with small numbers of devices, where the greater memory footprint of the latter is a problem.