Parse JSON find value and append random number?
<p>I have a JSON file with login and integer valuer (some result), like this:</p>

<pre><code>[
        {
           "name":"Tamara",
           "results":"434.545.234.664"
        },
        {
           "name":"Ted",
           "results":"434.545.234.664"
        }
]
</code></pre>

<p>I need to receive user login (name), find inserted name in JSON. If it already exists, add some number to the "results". </p>

<p>For example: If I input "Ted", some number will be appended to Ted's results like this: <code>"results":"434.545.234.664+4343}"</code></p>

<p>If name doesn't exist, add new record with:</p>

<pre><code>{
    "name":"new_name",
    "results":"some_number"
}
</code></pre>

<p>in it.</p>

<p>My code, which didn't work: </p>

<pre><code>with open('/Users/users_db.json') as jsonfile:
    user_name = ''
    while user_name == '':
        data = json.load(jsonfile)
        user_name = input('Your name: ')
        for user in data:
            if user_name in user['name']:
                print('Old user')
                break
            else:
                print('New user')
</code></pre>

<p>Here's one of the zillion possible ways to code your problem:</p>

<pre><code>import json
import random
import names

random.seed(1)

data = [
    {
        "name": "Tamara",
        "results": "434.545.234.664"
    },
    {
        "name": "Ted",
        "results": "434.545.234.664"
    }
]


def foo(lst, name):
    some_number = random.randint(0, 4343)
    values = filter(lambda d: d["name"] == name, lst)

    if values:
        for v in values:
            v["results"] += "+{0}".format(some_number)
    else:
        lst.append({
            "name": name,
            "results": some_number
        })

for name in ["Tamara", "Ted"] + [names.get_first_name() for i in range(8)]:
    foo(data, name)

print(data)
</code></pre>

<p>This one will use <a href="https://pypi.python.org/pypi/names/" rel="nofollow">names</a> module to generate random test names.</p>

<p>One advice though, take some time to read the help page, especially the sections named <a href="http://stackoverflow.com/help/on-topic">"What topics can I ask about here?"</a> and <a href="http://stackoverflow.com/help/dont-ask">"What types of questions should I avoid asking?"</a>. And more importantly, please read <a href="http://meta.stackexchange.com/q/156810/204922">the Stack Overflow question checklist</a>. You might also want to learn about <a href="http://stackoverflow.com/help/mcve">Minimal, Complete, and Verifiable Examples</a></p>

python json input