1

my Jenkins response is like this below: I want to iterate through all the values in the data and do the comparison like this

data = {"fruits":["apple","banana","spinach","carrot"]}

for value in data.json().values():
    if value[i] == apple or banana:
        print value[i] # i want to print if its find one or two
    elif value[i] == spinach or carrot:
        print value[i] # i want to print if it is true 

The code what i write is wrong but please help me out how to iterate through values and do the comparison and print the values if it exists am new to python

7
  • Where do you define i? Also you should fix your indentation here as white space is part of the code after all Commented Nov 12, 2018 at 2:07
  • alright guys it worked Thanks everyone @GreenCell Commented Nov 12, 2018 at 2:28
  • Start with an intro to loops in Python maybe? youtube.com/watch?v=6iF8Xb7Z3wQ&feature=youtu.be Commented Nov 12, 2018 at 2:31
  • @GreenCell can I use os.getenv in if statement for comparing like this if value == apple.banana12.(os.getenv).cdf or value == banana.56.(os.getenv).lkg print value? os.getenv is getting the data from Jenkins parameter so I want to insert the data inside the string to compare with the value? how can I do it in python? Commented Nov 12, 2018 at 2:38
  • As long as your environment variable with os.getenv evaluates to what you want then you can use anything. You might need to ask a new question or edit this question to clarify as it's getting off-topic and not very clear. Commented Nov 12, 2018 at 2:41

1 Answer 1

1

Here's what can fix it:

  • Make sure your data is a string
  • using json.loads
  • You don't need the index if you are iterating over the elements of

    import json
    data = json.loads('{"fruits":["apple","banana","spinach","carrot"]}')
    for value in data['fruits']:
        if value == 'apple' or 'banana':
            print(value) # i want to print if its find one or two
        elif value == 'spinach' or 'carrot':
            print(value) # i want to print if it is true 
    
Sign up to request clarification or add additional context in comments.

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.