json JMESPath array flatten + create key:value pair

txu3uszq  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(121)

我有一个JSON对象,我想把它变成一个字典。
对象如下所示:

{
  "data": {
    "votes": [
      {
        "id": "0x0d185b44dc6a9e4077d44a29fccc22d1f6238192107d08afa890a1bdddc01e10",
        "voter": "0x4Aad3544bE38067F534b6B516f9E645D495c4062"
      },
      {
        "id": "0xf141aa62dc57b20fb6ea1197df44ba0ab4de166c8c19302553065788812beea1",
        "voter": "0xB01f126C9041CC230e91378d688450Db4c3510cA"
      },
      {
        "id": "0xf141aa62dc57b20fb6ea1197df44ba0ab4de166c8c19302553065788812beea1",
        "voter": "0xB01f126C9041CC230e91378d688450Db4c3510cA"
      },
    ]
  }
}

我期待抓住选民的价值观,并创建一个像下面的对象。
我正在为首选结果射击,但如果它是不可能的,OK结果也可以。
OK结果
x一个一个一个一个x一个一个二个x
首选结果

"0x4Aad3544bE38067F534b6B516f9E645D495c4062": "1",
  "0xB01f126C9041CC230e91378d688450Db4c3510cA": "2",

迄今为止的测试:使用@|data.votes[].join(',',[voter, '1']),我可以得到一个接近的响应,例如"0x4Aad3544bE38067F534b6B516f9E645D495c4062,1",但不是我想要的。
请任何人都可以帮助无论是满意的结果或首选的结果?
编辑:使用@|data.votes[].join('":"',[voter, '1'])我现在有这个更好。

[
  "0x4Aad3544bE38067F534b6B516f9E645D495c4062\":\"1",
  "0xB01f126C9041CC230e91378d688450Db4c3510cA\":\"1",
  "0xB01f126C9041CC230e91378d688450Db4c3510cA\":\"1"
]
oknwwptz

oknwwptz1#

JmesPath无法计算列表中项目的出现频率。您必须使用其他工具。例如,

shell> cat data.json | jq '.data.votes[].voter' | uniq -c | awk '{ print $2, $1 }'
"0x4Aad3544bE38067F534b6B516f9E645D495c4062" 1
"0xB01f126C9041CC230e91378d688450Db4c3510cA" 2
  • 巨蟒
shell> cat count_voters.py 
#!/usr/bin/python3.9

import jmespath
import json
import yaml
import collections

with open('data.json', 'r') as myfile:
    data = myfile.read()
s = json.loads(data)
print(yaml.dump(s))

voters = jmespath.search('data.votes[*].voter', s)
print(yaml.dump(voters))

voters_freq = dict(collections.Counter(voters))
print(yaml.dump(voters_freq))

给予

shell> ./count_voters.py
data:
  votes:
  - id: '0x0d185'
    voter: '0x4Aad3544bE38067F534b6B516f9E645D495c4062'
  - id: '0xf141aa'
    voter: '0xB01f126C9041CC230e91378d688450Db4c3510cA'
  - id: '0xf141aa'
    voter: '0xB01f126C9041CC230e91378d688450Db4c3510cA'

- '0x4Aad3544bE38067F534b6B516f9E645D495c4062'
- '0xB01f126C9041CC230e91378d688450Db4c3510cA'
- '0xB01f126C9041CC230e91378d688450Db4c3510cA'

'0x4Aad3544bE38067F534b6B516f9E645D495c4062': 1
'0xB01f126C9041CC230e91378d688450Db4c3510cA': 2
  • 昂西布尔
shell> cat count_voters.yml
- hosts: localhost

  vars:

    voters: "{{ s.data.votes|json_query('[].voter') }}"
    voters_freq: "{{ voters|community.general.counter }}"

  tasks:

    - include_vars:
        file: data.json
        name: s
    - debug:
        var: s
    - debug:
        var: voters
    - debug:
        var: voters_freq

给予

shell> ansible-playbook count_voters.yml

PLAY [localhost] *****************************************************************************

TASK [include_vars] **************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  s:
    data:
      votes:
      - id: '0x0d185'
        voter: '0x4Aad3544bE38067F534b6B516f9E645D495c4062'
      - id: '0xf141aa'
        voter: '0xB01f126C9041CC230e91378d688450Db4c3510cA'
      - id: '0xf141aa'
        voter: '0xB01f126C9041CC230e91378d688450Db4c3510cA'

TASK [debug] *********************************************************************************
ok: [localhost] => 
  voters:
  - '0x4Aad3544bE38067F534b6B516f9E645D495c4062'
  - '0xB01f126C9041CC230e91378d688450Db4c3510cA'
  - '0xB01f126C9041CC230e91378d688450Db4c3510cA'

TASK [debug] *********************************************************************************
ok: [localhost] => 
  voters_freq:
    '0x4Aad3544bE38067F534b6B516f9E645D495c4062': 1
    '0xB01f126C9041CC230e91378d688450Db4c3510cA': 2

PLAY RECAP ***********************************************************************************
localhost: ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

相关问题