-1

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.

1 Answers1

0

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)
Bendik Knapstad
  • 1,409
  • 1
  • 9
  • 18