How do i delete an element of a json object with python? - python

I'm trying to delete elements from _notes that have _type as 1, but i keep getting an error and I'm not sure what it means, nor do I know how to fix it.. can anyone help me?
My trimmed JSON:
{
"_notes": [
{
"_time": 10,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 0,
"_cutDirection": 7
},
{
"_time": 12,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 1,
"_cutDirection": 1
},
{
"_time": 14,
"_lineIndex": 2,
"_lineLayer": 1,
"_type": 1,
"_cutDirection": 0
}
]
}
My python script:
#!/usr/bin/python3
import json
obj = json.load(open("ExpertPlusStandardd.dat"))
for i in range(len(obj["_notes"])):
print(obj["_notes"][i]["_type"])
if obj["_notes"][i]["_type"] == 1:
obj.pop(obj["_notes"][i])
open("test.dat", "w").write(
json.dumps(obj, indent=4, separators=(',', ': '))
)
Error:
Traceback (most recent call last): File "C:\programming\python\train_one_hand\run.py", line 9, in <module> obj.pop(obj["_notes"][i]) TypeError: unhashable type: 'dict'

It is usually a bad idea to delete from a list that you're iterating. Reverse iterating avoids some of the pitfalls, but it is much more difficult to follow code that does that, so usually you're better off using a list comprehension or filter.
obj["_notes"] = [x for x in obj["_notes"] if x["_type"] != 1]
This gives us the expected output :
{'_notes':
[
{
'_time': 10,
'_lineIndex': 2,
'_lineLayer': 0,
'_type': 0,
'_cutDirection': 7
}
]
}

Related

How would I be able to get a value from JSON?

I am trying to retrieve a value of a JSON file, but I keep getting the error
/home/pi/Desktop/asd.py:5: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
print(str(y[['tunnels']['public_url']]))
Traceback (most recent call last):
File "/home/pi/Desktop/asd.py", line 5, in <module>
print(str(y[['tunnels']['public_url']]))
TypeError: list indices must be integers or slices, not str
Here is my code:
import json
import requests
y = requests.get('http://localhost:4040/api/tunnels').content
x = json.loads(y)
print(str(y[['tunnels']['public_url']]))
And here is the JSON file from http://localhost:4040/api/tunnels:
{
"tunnels": [
{
"name": "command_line",
"uri": "/api/tunnels/command_line",
"public_url": "tcp://6.tcp.ngrok.io:18592",
"proto": "tcp",
"config": {
"addr": "localhost:25565",
"inspect": false
},
"metrics": {
"conns": {
"count": 166,
"gauge": 0,
"rate1": 0.017125393007699625,
"rate5": 0.027720847107677204,
"rate15": 0.02187693220653439,
"p50": 1971501053.5,
"p90": 11939627275.9,
"p95": 11991631962.25,
"p99": 19816396509.55
},
"http": {
"count": 0,
"rate1": 0,
"rate5": 0,
"rate15": 0,
"p50": 0,
"p90": 0,
"p95": 0,
"p99": 0
}
}
}
],
"uri": "/api/tunnels"
}
No matter how hard I try, I just can't seem to find a way to make this code work...
print(str(y[['tunnels']['public_url']]))
There is a pair of unnecessary brackets around ['tunnels']['public_url'] which causes it to be evaluated as a slice of y. That's what caused the exception, although removing the brackets will not help since y is a Response object.
Try the following:
import requests
resp = requests.get('http://localhost:4040/api/tunnels')
resp_json = resp.json()
print(resp_json['tunnels']['public_url'])
The requests Python module takes care of both retrieving JSON data and decoding it.
import requests
r = requests.get('http://localhost:4040/api/tunnels')
r.json()

Json file content extract and copy to excel/text

I have below JSON file from which I want to extract only
("workers": {"usersRunning": 1, "usersWaiting": 0, "total": 8, "jobsWaiting": 0, "inUse": 4})
part, and then put it into csv file or text file (tab deliminated). I am new to python so any help will be apprciated..
{
"workers": {
"usersRunning": 1,
"usersWaiting": 0,
"total": 8,
"jobsWaiting": 0,
"inUse": 4
},
"users": {
"activeUsers": 1,
"activity": [{
"maxWorkers": 4,
"inProgress": 4,
"displayName": "abc",
"waiting": 0
}]
}
}
I recommend using pandas. It has methods to read the json and you can use dataframe filtering to find the data you need.
Examples here: https://www.listendata.com/2019/07/how-to-filter-pandas-dataframe.html
I would recommend you use pandas and convert to excel
The following is sample that will help you to get your answer
json_ = {"workers": {"usersRunning": 1, "usersWaiting": 0, "total": 8, "jobsWaiting": 0, "inUse": 4}, "users": {"activeUsers": 1, "activity": [{"maxWorkers": 4, "inProgress": 4, "displayName": "abc", "waiting": 0}]}}
import pandas as pd
df = pd.DataFrame(data= json_['workers'], index=[0])
df.to_excel('json_.xlsx')

Json to Python converting

I have this sample json data, and need to grab only the MAC addresses so I can convert the mac to a list of manufacturers later.
[
{
"aps": {
"00:20:90:B3:16:25": {
"ssid": "",
"encryption": "Open",
"hidden": 1,
"channel": 11,
"signal": -23,
"wps": 0,
"last_seen": 1594356454,
"clients": []
},
"06:AA:A0:84:7F:D8": {
"ssid": "",
"encryption": "Open",
"hidden": 1,
"channel": 6,
"signal": -75,
"wps": 0,
"last_seen": 1594356452,
"clients": []
},
"1E:51:A4:D4:B7:29": {
"ssid": "",
"encryption": "WPA Mixed PSK (CCMP TKIP)",
"hidden": 1,
"channel": 11,
"signal": -63,
"wps": 0,
"last_seen": 1594356448,
"clients": []
}
}
}
]
This is my python program so far, but im not sure how to isolate the MAC address
import json
f = open('recon_data.json',)
data = json.load(f)
print(data["aps"])
f.close()
I get an error every time I run the program weather im asking for aps or ssid information
Traceback (most recent call last):
File "recon.py", line 12, in
print(data["ssid"])
TypeError: list indices must be integers or slices, not str
This is because the data you're loading is a list. Try data[0]["aps"]
As for getting all the mac addresses they are the keys in that dict so you can just use list on that inner dict to get all the keys:
import json
with open('recon_data.json') as f
data = json.load(f)
print(list(data[0]['aps']))
This will print a list of all the MAC addresses
['00:20:90:B3:16:25', '06:AA:A0:84:7F:D8', '1E:51:A4:D4:B7:29']

tf.train.SequenceExample with lists at each step

How is a list of values passed into a feature_list? The documentation suggests that this is valid but it is not clear how this is accomplished given that passing a list result in an error.
>>> tf.train.SequenceExample().feature_lists.feature_list["multiple"].feature.add().int64_list.value.append([1,2,3,4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal /containers.py", line 251, in append
self._values.append(self._type_checker.CheckValue(value))
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal /type_checkers.py", line 132, in CheckValue
raise TypeError(message)
TypeError: [1, 2, 3, 4] has type <type 'list'>, but expected one of: (<type 'int'>, <type 'long'>)
This is an example given in the example proto file.
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/example/example.proto
// Conditionally conformant FeatureLists, the parser configuration determines
// if the feature sizes must match:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0, 6.0 ] } } }
// } }
Is it necessary to use something other than append when adding a list?
An example of a sequence would be...
[[1,2,3],[4,5],[6,7],[8,9,10]]
...where there are four steps in this sequence, and at each step there is a set of values. The desired result would look something like the example below.
feature_lists: { feature_list: {
key: "movie_ratings"
value: { feature: { float_list: { value: [ 1, 2, 3 ] } }
feature: { float_list: { value: [ 4, 5 ] } }
feature: { float_list: { value: [ 6, 7 ] } }
feature: { float_list: { value: [ 8, 9, 10 ] } } }
} }
Use extend instead of append.
tf.train.SequenceExample().feature_lists.feature_list["multiple"].feature.add().int64_list.value.extend([1,2,3,4])

TypeError: list indices must be integers, not str,while parsing json

After submitting a request I have received the following json back:
{"type": [
{"ID": "all", "count": 1, "references": [
{ "id": "Boston,MA,02118", "text": "Boston,MA,02118", "val": "Boston,MA,02118", "type": 1 ,"zip": "02118","city": "Boston","state": "MA","lt": "42.3369","lg": "-71.0637","s": ""}
] }
] }
I captured the response in variable j and loaded it as follows,
l = json.loads(j)
Now I have:
>>> type(l)
<type 'dict'>
>>> l['type']['references']
Traceback (most recent call last):
File "C:\PyCharm\helpers\pydev\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
TypeError: list indices must be integers, not str
What am I doing wrong?
l['type'] gives you a list, not an object. So you have to access it like a list
l['type'][0]["references"]
The value of the key type is a list. Try doing type(l['type']) to confirm.
To access that value you would need to use:
l['type'][0]['references']
which would give you another list.

Categories