3

I want to parse following JSON with IPs and its counts into a single list containing only IPs using Python.

& code: `

response_ips= requests.get(url=link_getResults,headers=payload)
response_ips.raise_for_status()

data_ips= json.loads(response_ips.text)
results=data_ips['results']
print results
7
  • 5
    and your code so far? Commented Dec 26, 2017 at 8:55
  • Use json.loads() to parse it into a dictionary. Then split up the results element using str.split(). Commented Dec 26, 2017 at 9:00
  • ip is 168.9.64.246\t65098 or just 168.9.64.246? Commented Dec 26, 2017 at 9:01
  • remove the \n and \t first Commented Dec 26, 2017 at 9:08
  • @GarbageCollector just 168.9.64.246. Commented Dec 26, 2017 at 9:46

2 Answers 2

1

Split the results on \r\n to get each line, then split each of those on \t to separate the IP from the count.

ips = [line.split("\t")[0] for line in results.split("\r\n")]
print(ips)
Sign up to request clarification or add additional context in comments.

Comments

0

Since you have a JSON string, you first need to convert it to dict. After conversion, you need to escape \r\n otherwise JSON decoding will throw an error. Then, you need to split the results on \r\n and then on \t to get the IP.

Note that in your question the input is not enclosed in quotes. It should be if it is JSON.

import json
from functools import partial
from operator import is_not

j = '{"inline":true,"results":"168.9.64.246\t65098\r\n188.68.184.188, 10.178.156.22\t61168\r\n188.68.184.188, 10.178.156.58\t61050\r\n188.68.184.188, 10.178.156.24\t61039\r\n188.68.184.188, 10.178.156.31\t61038\r\n188.68.184.188, 10.178.156.35\t61036\r\n188.68.184.188, 10.178.156.44\t61023\r\n188.68.184.188, 10.178.156.34\t61013\r\n188.68.184.188, 10.178.156.71\t61012\r\n188.68.184.188, 10.178.156.33\t61008\r\n188.68.184.188, 10.178.156.40\t60985\r\n188.68.184.188, 10.178.156.36\t60976\r\n188.68.184.188, 10.178.156.43\t60974\r\n188.68.184.188, 10.178.156.26\t60962\r\n188.68.184.188, 10.178.156.61\t60960\r\n188.68.184.188, 10.178.156.66\t60958\r\n188.68.184.188, 10.178.156.68\t60956\r\n188.68.184.188, 10.178.156.52\t60942\r\n188.68.184.188, 10.178.156.49\t60940\r\n188.68.184.188, 10.178.156.53\t60921\r\n188.68.184.188, 10.178.156.48\t60898\r\n188.68.184.188, 10.178.156.45\t60857\r\n188.68.184.188, 10.178.156.29\t60851\r\n188.68.184.188, 10.178.156.69\t60844\r\n188.68.184.188, 10.178.156.74\t60840\r\n188.68.184.188, 10.178.156.60\t60838\r\n188.68.184.188, 10.178.156.55\t60834\r\n188.68.184.188, 10.178.156.63\t60829\r\n188.68.184.188, 10.178.156.30\t60827\r\n188.68.184.188, 10.178.156.67\t60825\r\n188.68.184.188, 10.178.156.62\t60814\r\n188.68.184.188, 10.178.156.64\t60793\r\n188.68.184.188, 10.178.156.50\t60784\r\n188.68.184.188, 10.178.156.54\t60781\r\n188.68.184.188, 10.178.156.25\t60777\r\n188.68.184.188, 10.178.156.39\t60759\r\n188.68.184.188, 10.178.156.65\t60758\r\n188.68.184.188, 10.178.156.42\t60750\r\n188.68.184.188, 10.178.156.73\t60744\r\n188.68.184.188, 10.178.156.51\t60731\r\n188.68.184.188, 10.178.156.27\t60725\r\n188.68.184.188, 10.178.156.23\t60718\r\n188.68.184.188, 10.178.156.28\t60715\r\n188.68.184.188, 10.178.156.37\t60703\r\n188.68.184.188, 10.178.156.72\t60690\r\n"}'
d = json.loads(j)
ips = list(filter(partial(is_not, ''),[ip.split('\t')[0] for ip in d['results'].split('\r\n')]))

The above code outputs:

['168.9.64.246', '188.68.184.188, 10.178.156.22', '188.68.184.188, 10.178.156.58', '188.68.184.188, 10.178.156.24', '188.68.184.188, 10.178.156.31', '188.68.184.188, 10.178.156.35', '188.68.184.188, 10.178.156.44', '188.68.184.188, 10.178.156.34', '188.68.184.188, 10.178.156.71', '188.68.184.188, 10.178.156.33', '188.68.184.188, 10.178.156.40', '188.68.184.188, 10.178.156.36', '188.68.184.188, 10.178.156.43', '188.68.184.188, 10.178.156.26', '188.68.184.188, 10.178.156.61', '188.68.184.188, 10.178.156.66', '188.68.184.188, 10.178.156.68', '188.68.184.188, 10.178.156.52', '188.68.184.188, 10.178.156.49', '188.68.184.188, 10.178.156.53', '188.68.184.188, 10.178.156.48', '188.68.184.188, 10.178.156.45', '188.68.184.188, 10.178.156.29', '188.68.184.188, 10.178.156.69', '188.68.184.188, 10.178.156.74', '188.68.184.188, 10.178.156.60', '188.68.184.188, 10.178.156.55', '188.68.184.188, 10.178.156.63', '188.68.184.188, 10.178.156.30', '188.68.184.188, 10.178.156.67', '188.68.184.188, 10.178.156.62', '188.68.184.188, 10.178.156.64', '188.68.184.188, 10.178.156.50', '188.68.184.188, 10.178.156.54', '188.68.184.188, 10.178.156.25', '188.68.184.188, 10.178.156.39', '188.68.184.188, 10.178.156.65', '188.68.184.188, 10.178.156.42', '188.68.184.188, 10.178.156.73', '188.68.184.188, 10.178.156.51', '188.68.184.188, 10.178.156.27', '188.68.184.188, 10.178.156.23', '188.68.184.188, 10.178.156.28', '188.68.184.188, 10.178.156.37', '188.68.184.188, 10.178.156.72']

Note: You also need to filter '' from list, because splitting on \r\n will produce '' as the last element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.