-7

I have Json:

[
 {
  "name":"Apple",
  "price":2,
  "have":0,
  "max":36
 },
 {
  "name":"Pineapple",
  "price":5,
  "have":6,
  "max":17
 }
]

I need the fastest function, that receives name, and sends price. For example for print(jsonname("Apple")) is 2.

P.S. Please do not post Loop answers, I know them. I need fast methods and names of methods

Hero Guy
  • 217
  • 1
  • 4
  • 11
  • 4
    What have you attempted so far? Please post your code. – James Jul 31 '17 at 11:02
  • No, the methods. that I know are slow. So I need fastest method – Hero Guy Jul 31 '17 at 11:03
  • 5
    @NurislomTuraev looks like you did not try anything – Arpit Solanki Jul 31 '17 at 11:04
  • 5
    @NurislomTuraev Be respective to others. Everyone is trying to help you but you have to show some effort that you made. SO is not a code writing service. Try googling before you ask question and last but not least always post your code – Arpit Solanki Jul 31 '17 at 11:08
  • @ArpitSolanki I didn't find anything about speed of parsing json. So I want to ask it here. Do you have something against to this? – Hero Guy Jul 31 '17 at 11:11

2 Answers2

31

Here’s an easy way to do it:

def function(json_object, name):
    for dict in json_object:
        if dict['name'] == name:
            return dict['price']

If you are sure that there are no duplicate names, an even more effective (and pythonic) way to do it is to use list comprehensions:

def function(json_object, name):
        return [obj for obj in json_object if obj['name']==name][0]['price']
Y2H
  • 2,419
  • 1
  • 19
  • 37
  • 2
    List comprehensions is always faster than a for loop in Python. You can read more on the topic if you wish. – Y2H Jul 31 '17 at 11:09
  • 1
    @Y2H list comprehensions always clean, fast and one liner. Good answer – Arpit Solanki Jul 31 '17 at 11:13
  • 1
    -1, list comprehension does not automatically make it more pythonic or faster, particularly when you misuse them as you have. In your loop example, you return as soon as you find a matching value, whereas in your list comprehension, you not only create an additional unnecessary list before returning, but worse, you must evaluate on the entire list before throwing it away and just picking the first match. – swhat Oct 15 '18 at 14:02
  • 7
    @Y2H are you serious? With the first method, if the first element of `json_object` matches `name`, you return immediately. With the second method, you return only after checking every element of `json_object`, which is substantial on large lists. I tested it myself, and worst case (search which matches the last element in `json_object`), the loop takes as long as the comprehension. Best case, where the search matches the first element, outperforms the comprehension... dramatically. Take another look at what your code actually does. – swhat Oct 15 '18 at 17:58
8
from json import loads

json = """[
 {
  "name":"Apple",
  "price":2,
  "have":0,
  "max":36
 },
 {
  "name":"Pineapple",
  "price":5,
  "have":6,
  "max":17
 }
]"""

parsedJson = loads (json)

def jsonname (name):
    for entry in parsedJson:
        if name == entry ['name']:
            return entry ['price']
marsouf
  • 1,107
  • 8
  • 15