#! /usr/bin/env python
import json, sys

def main():
    if sys.stdin.isatty():
        return print_help()

    lines = sys.stdin.readlines()
    raw_json = "".join( map( lambda x : x.strip(), lines ) )
    data = json.loads( raw_json )

    num_args = len( sys.argv )
    if num_args == 1:
        print_help()
    else:
        key = sys.argv[1]
        print( get_key( data, key ) )

def get_key( data, key ):
    keys = key.split( '.' )
    keys.reverse()
    intermediate_value = data
    while len( keys ):
        current_key = keys.pop()
        try:
            current_key = int(current_key)
        except ValueError: pass

        try:
            intermediate_value = intermediate_value[ current_key ]
        except IndexError:
            return 'undefined'

    return intermediate_value

def print_help():
    usage = """Usage:
    json-get # Show this help
    echo '{"test":"value"}' | json-get test # Print the key's value
    """
    print( usage )

if __name__ == '__main__': main()
