고쳐 만드는 느슨한 구조 JSON(이름 필드에 저장된 배)

0

질문

내가 독특한 문제를 내기 위해 어려움을 겪고있다면 내 머리를 감싸. 나는 조금의 JSON 에서는 API 입니다. 필드 이름이 실제로 저장된에서 .headerData.methodNames 고 가치의 대부분에 저장됩니다 .lightValueObjects[].데이터.

{
  "headerData": {
    "objectTypeName": "device",
    "methodNames": [
      "name",
      "accessIp",
      "deviceType",
      "version",
      "discoverTime",
      "discoverMethod",
      "status",
      "department",
      "description",
      "perfMonStatus",
      "eventLogStatus",
      "maintenance",
      "location",
      "agentStatus",
      "policyName",
      "policyId",
      "agentType",
      "templateIds",
      "agentId",
      "decommission",
      "accountId",
      "instanceId",
      "agentVersion",
      "parserName"
    ]
  },
  "lightValueObjects": [
    {
      "objectId": 17657,
      "custId": 1,
      "parentId": null,
      "collectorId": null,
      "data": [
        "super",
        "192.168.128.222",
        "leader",
        null,
        1637092568000,
        "LOG",
        1,
        "Super",
        null,
        "",
        "Normal",
        "",
        null,
        "",
        "",
        "",
        null,
        "",
        null,
        false,
        "",
        "",
        null,
        ""
      ],
      "extData": null,
      "naturalId": "lead%2dsuper",
      "aoSys": false
    },
    {
      "objectId": 883252,
      "custId": 1,
      "parentId": null,
      "collectorId": null,
      "data": [
        "fw01",
        "192.168.128.1",
        "fw",
        "ANY",
        1637098725000,
        "LOG",
        1,
        "Super",
        null,
        "",
        "Normal",
        "",
        null,
        "",
        "",
        "",
        null,
        "",
        null,
        false,
        "",
        "",
        null,
        ""
      ],
      "extData": null,
      "naturalId": "fw01",
      "aoSys": false
    }
  ],
  "errCode": 0,
  "errObj": null,
  "dataType": "Device",
  "totalCount": 0
}

다음 코드를 생산하고 더 나은 출력하지만,그것은 확실하지 않는 완벽합니다.

.headerData.methodNames as $header | [.lightValueObjects[].data] | map(
      . as $o |
      reduce .[] as $item({}; ($o | index($item)) as $index |
      .[$header[$index]] = (if $item == "" then null else $item end))
      )
[
  {
    "name": "super",
    "accessIp": "192.168.128.222",
    "deviceType": "leader",
    "version": null,
    "discoverTime": 1637092568000,
    "discoverMethod": "LOG",
    "status": 1,
    "department": "Super",
    "perfMonStatus": null,
    "eventLogStatus": "Normal",
    "decommission": false
  },
  {
    "name": "fw01",
    "accessIp": "192.168.128.1",
    "deviceType": "fw",
    "version": "ANY",
    "discoverTime": 1637098725000,
    "discoverMethod": "LOG",
    "status": 1,
    "department": "Super",
    "description": null,
    "perfMonStatus": null,
    "eventLogStatus": "Normal",
    "decommission": false
  }
]

현재의 문제

  1. 이 있어야 24 필드를 반환하는 장치 당지만,11 가 표시됩니다.
  2. 나는 할 수 없었던 그 밖으로 병합하는 방식 이름을 지정한 값이다.lightValueObjects 최종 출력됩니다.

여기에는 예입니다 내가 만들려고 노력하는 출력쇼(일을 유지하는 간단하고,나만 보여주는 1 장치)

[
  {
    "name": "super",
    "accessIp": "192.168.128.222",
    "deviceType": "leader",
    "version": null,
    "discoverTime": 1637092568000,
    "discoverMethod": "LOG",
    "status": 1,
    "department": "Super",
    "description": null,
    "perfMonStatus": null,
    "eventLogStatus": "Normal",
    "maintenance": null,
    "location": null,
    "agentStatus": null,
    "policyName": null,
    "agentType": null,
    "templateIds": null,
    "agentId": null,
    "decommission": false,
    "accountId": null,
    "instanceId": null,
    "agentVersion": null,
    "parserName": null,
    "objectId": 17657,
    "custId": 1,
    "parentId": null,
    "collectorId": null,
    "extData": null,
    "naturalId": "lead%2dsuper",
    "aoSys": false
  }
]

어떤 도움이 될 것이 크게 감사드립니다!!

jq
2021-11-23 17:56:07
1

최고의 응답

0

내가 알지 못하는 경우 나는 모두 당신의 오른쪽 정렬하지만,이 시도 하:

.headerData.methodNames as $header
| .lightValueObjects | map(
  ( [$header, .data]
    | transpose
    | map({(first): last})
    | add
  ) + del(.data)
)
[
  {
    "name": "super",
    "accessIp": "192.168.128.222",
    "deviceType": "leader",
    "version": null,
    "discoverTime": 1637092568000,
    "discoverMethod": "LOG",
    "status": 1,
    "department": "Super",
    "description": null,
    "perfMonStatus": "",
    "eventLogStatus": "Normal",
    "maintenance": "",
    "location": null,
    "agentStatus": "",
    "policyName": "",
    "policyId": "",
    "agentType": null,
    "templateIds": "",
    "agentId": null,
    "decommission": false,
    "accountId": "",
    "instanceId": "",
    "agentVersion": null,
    "parserName": "",
    "objectId": 17657,
    "custId": 1,
    "parentId": null,
    "collectorId": null,
    "extData": null,
    "naturalId": "lead%2dsuper",
    "aoSys": false
  },
  {
    "name": "fw01",
    "accessIp": "192.168.128.1",
    "deviceType": "fw",
    "version": "ANY",
    "discoverTime": 1637098725000,
    "discoverMethod": "LOG",
    "status": 1,
    "department": "Super",
    "description": null,
    "perfMonStatus": "",
    "eventLogStatus": "Normal",
    "maintenance": "",
    "location": null,
    "agentStatus": "",
    "policyName": "",
    "policyId": "",
    "agentType": null,
    "templateIds": "",
    "agentId": null,
    "decommission": false,
    "accountId": "",
    "instanceId": "",
    "agentVersion": null,
    "parserName": "",
    "objectId": 883252,
    "custId": 1,
    "parentId": null,
    "collectorId": null,
    "extData": null,
    "naturalId": "fw01",
    "aoSys": false
  }
]

데모

편집:바꾸려면 빈 문자열 ""null으로,당신의 접근 방식에서 교체 last 에서 내리 솔루션 중 (last | select(. != "") // null) 또,더 명시적으로 함 (last | if . == "" then null else . end).

2021-11-23 21:56:51

이처럼 작동합니다. 고 매우 깨끗이 나보다! 주의 할 점은,나의(선택(지속!= "")//null)하지만 대체로""훌륭한 기능들을 가지고 있습니다. 나는 끝까지 사용하여(는 경우 마지막==""그럼 null 다른 마지막)정말 감사드립니다!
Ken

내 나쁜할 수 있습니다 (last | select(. != "") // null). 하지만과 if-then-else-end 은 fine,too.
pmf

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................