-2

I was wondering how I can access a part of the json by looking through it and finding a name (hope the example makes more sense).

Name = 'kyle'

So I am trying to find kyle and all his information like his age.

{   "People": {
     "Names": {
       "kyle": [
         {
           "Age": "23",
           "Filler": "23"
         }
       ],
       "Michel": [
         {
           "value": "New"
         }
       ],
       "Smith": [
         {
           "value": "New"
         }
       ]
     }   
  } 
}

I later want to store this information into a variable. Any help will be much appreciated.

hqkhan
  • 463
  • 2
  • 10
Ali Al-Jabur
  • 77
  • 1
  • 9
  • I haven't found any solutions for my case everything i try isn't want i want or just doesn't work.So frankly not very much. – Ali Al-Jabur Jan 17 '19 at 22:14
  • 2
    Try looking into `json` library in python. Should be able to loop through all the items inside `Names` dict. Using `json` library, you can read in your data in a python `dict` which makes things simple to go through. – hqkhan Jan 17 '19 at 22:14
  • @AliAl-Jabur https://stackoverflow.com/a/2835672/10708112 <- might help. – hqkhan Jan 17 '19 at 22:16

3 Answers3

0

This should go straight forward i would say:

Information_list = json['Person']['names']['kyle']

Edit: its just nested dictionaries... however there exists also libraries as far as i know for easier handling if the json is larger

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
Prometheus
  • 82
  • 1
  • 8
  • The Json isn't very big probably a few hundred lines.Thanks for the help ill keep looking into it this helped a lot. – Ali Al-Jabur Jan 17 '19 at 22:21
0
import sys
import json

person_name=sys.argv[1]

json_str="""{"People": {"Names": {"kyle":[{"Age": "23", "Filler": "23" }], "Michel": [{"value": "New"}], "Smith": [ { "value": "New"}]}}}"""

json_data=json.loads(json_str)
for name in json_data["People"]["Names"]:
    if name == person_name:
        for data in json_data["People"]["Names"][name]:
            print("{}\n\nData: {}\n").format(person_name, data)

This can be customized to further suit your needs

usage:

./script_name.py <name_of_person>

scenario 1:

./script_name.py kyle

output 1:

Kyle:

Data: {u'Filler': u'23', u'Age': u'23'}

scenario 2:

./script_name.py Michel

output 2:

Michel

Data: {u'value': u'New'}

It will not longer look as nice since your data structures differ depending on the person, though it is easily possible to fix this.

jonroethke
  • 1,152
  • 2
  • 8
  • 16
0

You can do this to access the Name and Info:

d={ "People": { "Names": { "kyle": [ { "Age": "23", "Filler": "23" } ], "Michel": [ { "value": "New" } ], "Smith": [ { "value": "New" } ] } } }

for names in d['People'].values():
    for name, info in names.items():
        print()
        print('Name =', name)
        for i in info:
            for info1, info2 in i.items():
                print('info: ', info1, info2)

output:

Name = kyle
info:  Age 23
info:  Filler 23

Name = Michel
info:  value New

Name = Smith
info:  value New

Good luck!