修改JSON字段类型

s8vozzvw  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(117)

我有一个包含JSON格式数据的列表:

[
            {
                "avg_cpu": " 2.080901628850305",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 13.581080423266766",
                "avg_memory_deviation": " 7.848993362528565",
                "cpu_limits": " 0.0",
                "cpu_request": " 0.0",
                "dc_name": "db",
                "delta_cpu_limits": "  2.522309007407407  ",
                "delta_cpu_request": "  2.080901628850305  ",
                "delta_memory_limits": " -493.33203125  ",
                "delta_memory_request": " -493.89189276897764 ",
                "max_cpu": " 2.522309007407407",
                "max_memory": " 18.66796875",
                "memory_limits": " 512.0",
                "memory_request": " 512.0",
                "namespace": "voting-app-1",
                "replicas": " 1.0"
            },
            {
                "avg_cpu": " 12.842575355202916",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 8.329453915867436",
                "avg_memory_deviation": " 0.17931347295084302",
                "cpu_limits": " 0.0",
                "cpu_request": " 0.0",
                "dc_name": "redis",
                "delta_cpu_limits": "  1044.5020826481482  ",
                "delta_cpu_request": "  12.842575355202916  ",
                "delta_memory_limits": " -501.12109375  ",
                "delta_memory_request": " -503.6705460841326 ",
                "max_cpu": " 1044.5020826481482",
                "max_memory": " 10.87890625",
                "memory_limits": " 512.0",
                "memory_request": " 512.0",
                "namespace": "voting-app-1",
                "replicas": " 1.0"
            },
            {
                "avg_cpu": " 2.0510519901995226",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 13.58236662375085",
                "avg_memory_deviation": " 7.85209673059425",
                "cpu_limits": " 300.0",
                "cpu_request": " 200.0",
                "dc_name": "db",
                "delta_cpu_limits": " -297.6661470962963 ",
                "delta_cpu_request": " -197.94886067161377 ",
                "delta_memory_limits": " -488.47265625  ",
                "delta_memory_request": " -493.8901778349989 ",
                "max_cpu": " 2.333852903703715",
                "max_memory": " 23.52734375",
                "memory_limits": " 512.0",
                "memory_request": " 306.0",
                "namespace": "voting-app-10",
                "replicas": " 1.0"
            }
]

我想用Ansible对它们进行排序,但所有字段都是字符串,排序显然不正确。为了做到这一点,我会这样做:

- name: divide json by resource
  set_fact:
    cpu_list: "{{ data_json.list|sort(attribute='max_cpu')| list }}"

我想知道是否有一种方法可以在排序之前将json中的字段从string修改为float,等待我确信排序将很好地工作。
提前感谢您的帮助,问候!

owfi6suc

owfi6suc1#

将字符串转换为数字并创建列表 data_update

data_update: "{{ data_json.list|
                   json_query('[].{max_cpu: to_number(max_cpu)}') }}"

给予

data_update:
  - max_cpu: 2.522309007407407
  - max_cpu: 1044.5020826481482
  - max_cpu: 2.333852903703715

更新字典列表并按属性排序

data_sorted: "{{ data_json.list|
                   zip(data_update)|
                   map('combine')|
                   sort(attribute='max_cpu') }}"

给予

data_sorted:
  - avg_cpu: ' 2.0510519901995226'
    avg_cpu_deviation: ' 0.0'
    avg_memory: ' 13.58236662375085'
    avg_memory_deviation: ' 7.85209673059425'
    cpu_limits: ' 300.0'
    cpu_request: ' 200.0'
    dc_name: db
    delta_cpu_limits: ' -297.6661470962963 '
    delta_cpu_request: ' -197.94886067161377 '
    delta_memory_limits: ' -488.47265625  '
    delta_memory_request: ' -493.8901778349989 '
    max_cpu: 2.333852903703715
    max_memory: ' 23.52734375'
    memory_limits: ' 512.0'
    memory_request: ' 306.0'
    namespace: voting-app-10
    replicas: ' 1.0'
  - avg_cpu: ' 2.080901628850305'
    avg_cpu_deviation: ' 0.0'
    avg_memory: ' 13.581080423266766'
    avg_memory_deviation: ' 7.848993362528565'
    cpu_limits: ' 0.0'
    cpu_request: ' 0.0'
    dc_name: db
    delta_cpu_limits: '  2.522309007407407  '
    delta_cpu_request: '  2.080901628850305  '
    delta_memory_limits: ' -493.33203125  '
    delta_memory_request: ' -493.89189276897764 '
    max_cpu: 2.522309007407407
    max_memory: ' 18.66796875'
    memory_limits: ' 512.0'
    memory_request: ' 512.0'
    namespace: voting-app-1
    replicas: ' 1.0'
  - avg_cpu: ' 12.842575355202916'
    avg_cpu_deviation: ' 0.0'
    avg_memory: ' 8.329453915867436'
    avg_memory_deviation: ' 0.17931347295084302'
    cpu_limits: ' 0.0'
    cpu_request: ' 0.0'
    dc_name: redis
    delta_cpu_limits: '  1044.5020826481482  '
    delta_cpu_request: '  12.842575355202916  '
    delta_memory_limits: ' -501.12109375  '
    delta_memory_request: ' -503.6705460841326 '
    max_cpu: 1044.5020826481482
    max_memory: ' 10.87890625'
    memory_limits: ' 512.0'
    memory_request: ' 512.0'
    namespace: voting-app-1
    replicas: ' 1.0'

完整的测试剧本示例

- hosts: localhost

  vars:

    data_json:
      list:
        [
            {
                "avg_cpu": " 2.080901628850305",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 13.581080423266766",
                "avg_memory_deviation": " 7.848993362528565",
                "cpu_limits": " 0.0",
                "cpu_request": " 0.0",
                "dc_name": "db",
                "delta_cpu_limits": "  2.522309007407407  ",
                "delta_cpu_request": "  2.080901628850305  ",
                "delta_memory_limits": " -493.33203125  ",
                "delta_memory_request": " -493.89189276897764 ",
                "max_cpu": " 2.522309007407407",
                "max_memory": " 18.66796875",
                "memory_limits": " 512.0",
                "memory_request": " 512.0",
                "namespace": "voting-app-1",
                "replicas": " 1.0"
            },
            {
                "avg_cpu": " 12.842575355202916",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 8.329453915867436",
                "avg_memory_deviation": " 0.17931347295084302",
                "cpu_limits": " 0.0",
                "cpu_request": " 0.0",
                "dc_name": "redis",
                "delta_cpu_limits": "  1044.5020826481482  ",
                "delta_cpu_request": "  12.842575355202916  ",
                "delta_memory_limits": " -501.12109375  ",
                "delta_memory_request": " -503.6705460841326 ",
                "max_cpu": " 1044.5020826481482",
                "max_memory": " 10.87890625",
                "memory_limits": " 512.0",
                "memory_request": " 512.0",
                "namespace": "voting-app-1",
                "replicas": " 1.0"
            },
            {
                "avg_cpu": " 2.0510519901995226",
                "avg_cpu_deviation": " 0.0",
                "avg_memory": " 13.58236662375085",
                "avg_memory_deviation": " 7.85209673059425",
                "cpu_limits": " 300.0",
                "cpu_request": " 200.0",
                "dc_name": "db",
                "delta_cpu_limits": " -297.6661470962963 ",
                "delta_cpu_request": " -197.94886067161377 ",
                "delta_memory_limits": " -488.47265625  ",
                "delta_memory_request": " -493.8901778349989 ",
                "max_cpu": " 2.333852903703715",
                "max_memory": " 23.52734375",
                "memory_limits": " 512.0",
                "memory_request": " 306.0",
                "namespace": "voting-app-10",
                "replicas": " 1.0"
            }
        ]

    data_update: "{{ data_json.list|json_query('[].{max_cpu: to_number(max_cpu)}') }}"
    data_sorted: "{{ data_json.list|zip(data_update)|
                                    map('combine')|
                                    sort(attribute='max_cpu') }}"
        
  tasks:
        
    - debug:
        var: data_update
    - debug:
        var: data_sorted

相关问题