1

My question exactly is i want to use function output as input to another function,

Like:

def first(one,two):
    #some codes gives me info from json files
    api_data = json.loads(api.text)
    return api_data["response"]["info"][one][two];

def second(moreinfo):
    #this code is using first function and gives me the result to do something else on it, beside i want the first function as it is, because i'm using it in the project.
    api_data = json.loads(api.text)
    return api_data[moreinfo]["data"]["name"];

I'm using this in the file to get the result from second function

second(""+str(first(one,"two"))+"")

And i got this error

return api_data[first(one,"two")]["data"]["name"]
KeyError: 'data'

I think this error because first(one,"two") in second function doesn't took, because i tried to put

return api_data["1"]["data"]["name"] 

And its work with giving me info for number 1 result from first function,

Thanks and Regards :)

EDITED

Some examples for json

(for first function)

{
    "response": {
        "info": [
            {
                "id": 1,
                "desc": "desc"
            }
        ]
    }
}

Second example (i want to print this in second function)

{
  "1": {
    "data": {
      "name": "Name"
    }
  }
}
11
  • Can you print api_data? Commented Sep 26, 2017 at 4:49
  • Yes, can you show example api_data contents? Commented Sep 26, 2017 at 4:51
  • Did you try second(str(first(one,"two"))) Commented Sep 26, 2017 at 4:51
  • @Jack Hughes, Ok, Now examples in question, Commented Sep 26, 2017 at 5:40
  • @AndMar Yes i tried it, nothing change, same problem Commented Sep 26, 2017 at 5:40

2 Answers 2

2

There are two approaches that either change the code as per your json response or change the json response on the basis of your code.

1st approach:

def first(index):
    return api_data["response"]["info"][index]['id']

def second(moreinfo):
    return api_data[moreinfo]["data"]["name"]

JSON:

api_data = {'1': {'data': {'name': 'name'}}, 'response': {'info': [{"id": 1, "name": "name"}]}}

Function Call:

second(""+str(first(0))+"")

2nd approach: Based on your given code JSON should be like this.

api_data = {'1': {'data': {'name': 'name'}}, 'response': {'info': {'one': {'two': 1}}}}

Function Call:

second(""+str(first('one', 'two'))+"")
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply but doesn't help me, and My first function take 2 variable, because giving 2 deferent output from it,
But it is not possible to get value of key "one" from json because your json does not have key of 'one' or 'two'.
No JSON have what i want, and as said before, when tried to put number str(first('one', 'two')) its get all info i want from number i put.
But how would you get a value against 'one' or 'two' key if you are not giving it in json? key must be defined in json.
if you was saying about this, str(first('one', 'two')) one is id of what i want to get and two is what i want to get (ID or Name), str(first(1, "name")) this code print the name of what i want.
|
1

The problem when i use PyQt, but if i just try the code without PyQt its work fine so, the question is no longer not solved.

def first(one,two):
    api = requests.get("url")
    api_data = json.loads(api.text)
    return api_data["response"]["info"][one][two]

def second(id):
    api = requests.get("another-url")
    api_data = json.loads(api.text)
    return api_data[id]["data"]["name"]

print(second(str(first("1","id"))))

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.