0

I am looking for more info regarding this issue I have. So far I have checked the JSON encoding/decoding but it was not precisely what I was looking for.

I am looking for some way to strip this kind of list quite easily:

//response
{
  "age":[
    {"#":"1","age":10},
    {"#":"2","age":12},
    {"#":"3","age":16},
    {"#":"4","age":3}
  ],
  "age2":[
    {"#":"1","age":10},
    {"#":"2","age":12},
    {"#":"3","age":16},
    {"#":"4","age":3}
  ],
  "days_month":31,
  "year":2017
}

So how do I easily extract the data? i.e. I want to get the result age of person in age2 with # == 3.

To get the results for year/days_months I found the solution with google:

j=json.loads(r.content)
print(j['year'])

to retrieve the data. Probably I have missed something somewhere on the internet, but I could not find the specific solution for this case.

martineau
  • 119,623
  • 25
  • 170
  • 301
HME
  • 103
  • 8
  • 1
    `print(j['age2']|2]['#'])` should do it – Jean-François Fabre Jul 29 '17 at 21:13
  • 1
    What you get when you do `j=json.loads(...)` is a python `dict`. You can treat it as with any other dict. Please read a little bit on the web about dicts, you'll find them easy to use. Then, please edit your question in terms of dicts. It will be much easier to understand. – matiasg Jul 29 '17 at 22:07

2 Answers2

0

I think this is what @Jean-François Fabre tried to indicate:

import json

response = """
    {
      "age":[
        {"#":"1","age":10},
        {"#":"2","age":12},
        {"#":"3","age":16},
        {"#":"4","age":3}
      ],
      "age2":[
        {"#":"1","age":10},
        {"#":"2","age":12},
        {"#":"3","age":16},
        {"#":"4","age":3}
      ],
      "days_month":31,
      "year":2017
    }
"""

j = json.loads(response)
# note that the [2] means the third element in the "age2" list-of-dicts
print(j['age2'][2]['#'])  # -> 3
print(j['age2'][2]['age'])  # -> 16

json.loads() converts a string in JSON format into a Python object. In particular it converts JSON objects into Python dictionaries and JSON lists into Python list objects. This means you can access the contents of the result stored in the variable j in this case, just like you would if it was a native mixture of one or more of those types of Python datatypes (and would look very similar to what is shown in the response).

martineau
  • 119,623
  • 25
  • 170
  • 301
0

As the search criterion you are looking for is not contained in the indices of the respective datastructures, I would do it using a list comprehension. For your example, this would be

[person['age'] for person in j['age2'] if person['#'] == u'3'][0]

This iterates through all the items in the list under 'age2', and puts all the items where the number is '3' into a list. The [0] selects the first entry of the list.

However, this is very inefficient. If you have large datasets, you might want to have a look at pandas:

df = pandas.DataFrame(j['age2'])
df[df['#'] == '3']['age']

which is much more performant as long as your data can be represented by a sort of series or table.

C. Nitschke
  • 199
  • 6