Monday, October 25, 2021

[SOLVED] How to grep information for twitter?

Issue

I'd like to get the output "tester123" if I have the following input:

[{"id":4133554,"id_str":"445444","name":"tester123","screen_name":"whatelse"}]

Is that possible with the grep command?


Solution

I know that you said grep, but your input is clearly JSON which grep is not really equipped to parse. You could use a quick Python solution:

import json
inText = '[{"id":4133554,"id_str":"445444","name":"tester123","screen_name":"whatelse"}]'
data = json.loads(inText)
print data[0]['name']

You could get input in other ways based on your input format. For example, by reading from a file or via stdin:

# Input file
with open('file.json') as fp:
     inText = fp.read()
# stdin
inText = raw_input()


Answered By - bytesized