0

I am trying to retrieve the urls from the sub elements given comp1 and comp2 as input to the python script

{
  "main1": {
    "comp1": {
      "url": [
        "http://kcdclcm.com",
        "http://dacklsd.com"
       ]
    },
    "comp2": {
      "url": [
           "http://dccmsdlkm.com",
           "http://clsdmcsm.com"
        ]
    }
  },
  "main2": {
    "comp3": {
      "url": [
        "http://csdc.com",
        "http://uihjkn.com"
       ]
    },
    "comp4": {
      "url": [
           "http://jkll.com",
           "http://ackjn.com"
        ]
    }
  }
}

Following is the snippet of the python function, I am trying to use to grab the urls

import json
data = json.load(open('test.json'))
def geturl(comp):
    if comp in data[comp]:
        for url in data[comp]['url']:
            print url
geturl('comp1')
geturl('comp2')

I totally understand the error is in the 4th and 5th line of the script, since i am trying to grab the url information from the second element of the json data without passing the first element 'main1' or 'main2'. Same script works fine if I replace the 4th and 5th line as below:

if comp in data['main1']:
    for url in data['main1'][comp]['url']:

In my case, i would not know main1 and main2 as the user would just pass comp1, comp2, comp3 and comp4 part as input to the script. Is there a way to find the url information given that only the second element is known

Any inputs would be highly appreciated.

2 Answers 2

1

You need to iterate through the keys/values in the dict to check if the second level key you are searching for is present:

import json

data = json.load(open('test.json'))

def geturl(comp):
    for k, v in data.items():
        if comp in v and 'url' in v[comp]:
            print "%s" % "\n".join(v[comp]['url'])

geturl('comp1')
geturl('comp2')
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to search the urls with only comp key in every main, you just need to do it like this:

import json
data = json.load(open('test.json'))
def geturl(comp):
    for mainKey in data:
        main = data[mainKey]
        if comp in main:
            urls = main[comp]['url']
            for url in urls:
                print url

geturl('comp1')
geturl('comp2')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.