将 yaml 转换为 json 有效载荷 - 迭代 yaml 对象并具有不同的 json 有效载荷

convert yaml to json payload - iterate over yaml objects and have different json payloads

我发现我可以很容易地将 yaml 转换为 json。这将使我能够从 yaml 文件中创建和发送有效载荷,并在那里进行更新。

我想要完成什么? 理想情况下,我需要迭代的 yaml 文件中有多个对象。每次我迭代它们时,它们都需要作为 api 请求的有效负载发送。我想将 yaml 转换为 json 并将其作为有效载荷提供,而不是执行 for 循环并手动设置所有值。

sample.yaml

Component: 'users'
user:
  username: 'rhendricks'
  email: 'rich@piedpiper.com'
  receiveNotifications: False
  firstName: 'Richard'
  lastName: 'Hendricks'
  password: 'Pass321!'
  roles: 
    - id: 105
    - id: 106 
user:
  username: 'dinesh'
  email: 'dinesh@piedpiper.com'
  receiveNotifications: False
  firstName: 'Dinesh'
  lastName: 'Chugtai'
  password: 'Pass321!'
  roles:
    - id: 105

以上是我正在使用的示例 yaml 文件。

api 有效载荷期望的内容:

spayload = json.dumps({
                'user':{
                    'username': username,
                    'email': email,
                    'firstName': firstName,
                    'lastName': lastName,
                    'password': password,
                    'recieveNotifications': receiveNotifications,
                    'roles': [
                        {'id': 106},
                        {'id': 105}
                    ]
                }
            })

我目前设置有效负载的方法类似于以下内容 - 这是另一个有效负载的示例。如果我想不出另一种方法,我将得出以下结果。

 for k, v in result['groups'].items():
        name = result['groups'][k]['name']
        description = result['groups'][k]['description']
        location = result['groups'][k]['location']
        code = result['groups'][k]['code']
        payload = json.dumps({
                                "group":{
                                  "name": name,
                                  "description": description,
                                  "location": location,
                                  "code": code
                                }
                              })

这是我目前的测试:

import json
import re
import urllib
import urllib3
import yaml
import requests
import glob
from . import morph_log
def createUser(yaml_file):
    logger = morph_log.get_logger('createUser')
    files = glob.glob(yaml_file)
    for file in files: 
        yaml_file = file 
        logger.info('Current file: '+yaml_file)

        with open(yaml_file) as f:
            try:
                result=yaml.safe_load(f)
            except yaml.YAMLError as exc:
                logger.error(exc)
                logger.error('Was unable to load the file')
        os_list={}
        with open(yaml_file) as infile:
            os_list = yaml.load(infile, Loader=yaml.FullLoader)
        print(json.dumps(os_list))
        yaml_file = open(yaml_file, 'r')
        result = yaml_file.readlines()
        del result[0]
        print(result)
       # for key, value in yaml.load(result, Loader=yaml.FullLoader).items():
      #      print('key', key)

        for value in result:
            print('Entry', value)
            #print('entry:', value['user'])
            #print(entry['username'])
            #print('Sub', entry['users'])

        for key, value in result['user'].items():
           # username = value['username']
            print('Key', key)
            print('User', value )

我还试图在开始处理时删除组件行,因为 api 不理解该构造。我使用该组件元素作为正确处理文件的方式。

示例 yaml 文件作为我需要能够发送的 2 个有效负载。

这是我到目前为止的日志。

2022-02-04 21:51:43,585:25:users:createUser:INFO:Current file: contentpacks/piedPiper/users.yaml
{"Component": "users", "user": {"username": "dinesh", "email": "dinesh@piedpiper.com", "receiveNotifications": false, "firstName": "Dinesh", "lastName": "Chugtai", "password": "Pass321!", "roles": [{"id": 105}]}}
['user:\n', "  username: 'rhendricks'\n", "  email: 'rich@piedpiper.com'\n", '  receiveNotifications: False\n', "  firstName: 'Richard'\n", "  lastName: 'Hendricks'\n", "  password: 'Pass321!'\n", '  roles: \n', '    - id: 105\n', '    - id: 106    # needs to be some sort of list. \n', 'user:\n', "  username: 'dinesh'\n", "  email: 'dinesh@piedpiper.com'\n", '  receiveNotifications: False\n', "  firstName: 'Dinesh'\n", "  lastName: 'Chugtai'\n", "  password: 'Pass321!'\n", '  roles:\n', '    - id: 105']
Entry user:

Entry   username: 'rhendricks'

Entry   email: 'rich@piedpiper.com'

Entry   receiveNotifications: False

Entry   firstName: 'Richard'

Entry   lastName: 'Hendricks'

Entry   password: 'Pass321!'

Entry   roles: 

Entry     - id: 105

Entry     - id: 106    # needs to be some sort of list. 

Entry user:

Entry   username: 'dinesh'

Entry   email: 'dinesh@piedpiper.com'

Entry   receiveNotifications: False

Entry   firstName: 'Dinesh'

Entry   lastName: 'Chugtai'

Entry   password: 'Pass321!'

Entry   roles:

Entry     - id: 105
Traceback (most recent call last):
  File "app.py", line 21, in <module>
    cpack_utility.contentPack_file_processor(contentPackSelection)
  File "/home/ubuntu/implementation/cpack_utility/contentPack_processing.py", line 149, in contentPack_file_processor
    contentPack_implementation(contentPackSelection)
  File "/home/ubuntu/implementation/cpack_utility/contentPack_processing.py", line 62, in contentPack_implementation
    users.createUser(yaml_file)
  File "/home/ubuntu/implementation/cpack_utility/users.py", line 50, in createUser
    for key, value in result['user'].items():
TypeError: list indices must be integers or slices, not str

因为我希望有效载荷是某种方式,所以我无疑必须将其添加到我的 yaml 验证脚本中。那部分很简单。

如有任何想法,我们将不胜感激。

在 YAML 中,映射中有多个相同的键是错误的。您的根映射有 user 两次。这是不允许的。

很难弄清楚你想做什么,但我相信你正在搜索 multi-document YAML 文件:

Component: 'users'
---
user:
  username: 'rhendricks'
  email: 'rich@piedpiper.com'
  receiveNotifications: False
  firstName: 'Richard'
  lastName: 'Hendricks'
  password: 'Pass321!'
  roles: 
    - id: 105
    - id: 106
---
user:
  username: 'dinesh'
  email: 'dinesh@piedpiper.com'
  receiveNotifications: False
  firstName: 'Dinesh'
  lastName: 'Chugtai'
  password: 'Pass321!'
  roles:
    - id: 105

--- 是一个 指令结束标记 ,在这种情况下有效地分隔多个 YAML 文档。每个文档都包含一个带有唯一键 user.

的根映射

这个最小脚本:

import yaml, json
import sys

with open("sample.yaml") as file:
    content = iter(yaml.safe_load_all(file))
    next(content) # skip first document
    for doc in content:
        json.dump(doc, sys.stdout, indent=2)
        print("\n---")

然后会产生:

{
  "user": {
    "username": "rhendricks",
    "email": "rich@piedpiper.com",
    "receiveNotifications": false,
    "firstName": "Richard",
    "lastName": "Hendricks",
    "password": "Pass321!",
    "roles": [
      {
        "id": 105
      },
      {
        "id": 106
      }
    ]
  }
}
---
{
  "user": {
    "username": "dinesh",
    "email": "dinesh@piedpiper.com",
    "receiveNotifications": false,
    "firstName": "Dinesh",
    "lastName": "Chugtai",
    "password": "Pass321!",
    "roles": [
      {
        "id": 105
      }
    ]
  }
}
---