简体   繁体   中英

Converting YAML dict into key/value pair list

I am trying to pass a YAML file into a python script that compares the YAML file to a list of key:value pairs grabbed from an API.

YAML file

required_tags:
  environment:
    - "dev"
    - "qa"
  owner:
    - "*"

key:values from API

{
  "environment": "prod",
  "owner": "billy"
}

So I am trying to say, if the key:values from the API don't match any of the required_tags from the YAML file, send a message with the key:value pair that's wrong. So in this case, "environment": "prod" does not match any of the "environment" values from the required_tags section, so I would like to print the violating key:value pair. I was thinking perhaps I have to generate a list of key:values from the YAML first to make them consistent with the data structure of the API call? So the YAML would look more like this?

{
  "environment":"dev",
  "environment":"qa",
  "owner":"*"
}

Still new to some of the dictionary/tuple stuff, so any help would be appreciated.

For data consistency, you should preserve the data structure of the YAML in the dictionary like this:

{
  'environment': ['dev','qa'],
  'owner': '*'
}

So when validating the API_key:API_value of API, you can check the data types of YAML_value then make the right action.

If YAML_value is a list, check if the list contains API_value , or else compare API_value & YAML_value directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM