0

I have a json object saved inside test_data and I need to know if the string inside test_data['sign_in_info']['package_type'] contains the string "vacation_package" in it. I assumed that in could help but I'm not sure how to use it properly or if it´s correct to use it. This is an example of the json object:

"checkout_details": {
        "file_name" : "pnc04",
        "test_directory" : "test_pnc04_package_today3_signedout_noinsurance_cc",
        "scope": "wdw",
          "number_of_adults": "2",
          "number_of_children": "0",
          "sign_in_info": {
              "should_login": false,
              **"package_type": "vacation_package"**
          },

package type has "vacation_package" in it, but it's not always this way. For now I´m only saving the data this way:

package_type = test_data['sign_in_info']['package_type']

Now, is it ok to do something like:

p= "vacation_package"
if(p in package_type):
    ....

Or do I have to use 're' to cut the string and find it that way?

1
  • is test_data['sign_in_info']['package_type'] == 'vacation_package' not appropriate? Commented Mar 26, 2015 at 20:50

2 Answers 2

1

You answer depends on what exactly you expect to get from test_data['sign_in_info']['package_type']. Will 'vacation_package' always be by itself? Then in is fine. Could it be part of a larger string? Then you need to use re.search. It might be safer just to use re.search (and a good opportunity to practice regular expressions).

Sign up to request clarification or add additional context in comments.

2 Comments

That's my concern. "vacation_package" may noy always be alone, sometimes comes with something like "vacation_package_tickets". So re would be a good solution?
Using re.search would definitely work. You might be able to use just ==, but that would fail in the case of something like "vacation_package, vacation_package_tickets".
0

No need to use re, assuming you are using the json package. Yes, it's okay to do that, but are you trying to see if there is a "package type" listed, or if the package type contains vacation_package, possibly among other things? If not, this might be closer to what you want, as it checks for exact matches:

import json
data = json.load(open('file.json'))

if data['sign_in_info'].get('package_type') == "vacation_package":
   pass # do something

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.