简体   繁体   中英

How do print a part of a json file python

How do I print a part of a json file


[
    {   "ip": "192.168.0.135",   "timestamp": "1662977246", "ports": [ {"port": xxx, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 64} ] }
    ,
    {   "ip": "192.168.0.136",   "timestamp": "1662977246", "ports": [ {"port": xxx, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 64} ] }
]

In this case I want to save all the ips from the file into another json.

A json object is pretty much a list of dictionaries in your case.

so you can iterate over your object an pick the info you want

your_json_object
new_json_object = []
for i in your_json_object:
    new_json_object.append[i["ip"]]

or with a list comprehension

new_json_object = [i["ip"] for i in your_json_object]

to save it:

with open("myfile.json", "w") as file:
    json.dump(new_json_object, file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM