2017-12-22 7 views
2

데이터 집합이 많습니다. JQ를 사용하여 레코드에 관심이있는 데이터 만 포함하는 개체를 구성하고 있습니다. 내 문제는 내가 중복 된 개체를 볼 시작, 내 구문이 잘못된 것 같습니다.중첩 된 개체의 필드에서 JQ 필터링

플랫 필드와 하위 오브젝트 배열이 포함 된 개체로 작업하고 있는데 원하는 모든 데이터가 포함 된 새 개체를 만들 특정 필드가 있습니다. 일부 플랫 필드와 배열 객체의 일부 필드를 포함합니다. 여기

내가 실행 문제를 tmpData.json

{ 
"id": "0001", 
"type": "donut", 
"name": "Cake", 
"ppu": 0.55, 
"batter": [{ 
     "id": "1001", 
     "type": "Regular" 
    }, 
    { 
     "id": "1002", 
     "type": "Chocolate" 
    }, 
    { 
     "id": "1003", 
     "type": "Blueberry" 
    }, 
    { 
     "id": "1004", 
     "type": "Devil's Food" 
    } 
] 
} 

을 보여 도움이 작은 샘플입니다이 : 객체의 비 JSON 세트 (이 쉼표를 실종)

{ 
    "id": "0001", 
    "type": "donut", 
    "batter": "1001" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batter": "1002" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batter": "1003" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batter": "1004" 
} 
를 출력 cat tmpData.txt | jq {'id: .id, type: .type, batter: .batter[].id'}

좋습니다. 나는 이제 각각 부모 ID 0001을 포함하는 객체를 가지며 배열의 다른 항목이 각 객체에 연결됩니다.

나는 실행하면 : 내가 잘못 항목

{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1001", 
    "batterType": "Regular" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1001", 
    "batterType": "Chocolate" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1001", 
    "batterType": "Blueberry" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1001", 
    "batterType": "Devil's Food" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1002", 
    "batterType": "Regular" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1002", 
    "batterType": "Chocolate" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1002", 
    "batterType": "Blueberry" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1002", 
    "batterType": "Devil's Food" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1003", 
    "batterType": "Regular" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1003", 
    "batterType": "Chocolate" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1003", 
    "batterType": "Blueberry" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1003", 
    "batterType": "Devil's Food" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1004", 
    "batterType": "Regular" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1004", 
    "batterType": "Chocolate" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1004", 
    "batterType": "Blueberry" 
} 
{ 
    "id": "0001", 
    "type": "donut", 
    "batterID": "1004", 
    "batterType": "Devil's Food" 
} 

을 연결 중복을 많이 얻을 추가 type 필드와 cat tmpData.txt | jq {'id: .id, type: .type, batterID: .batter[].id, batterType: .batter[].type'}

지금은 각 batterID는 모든 종류의 regular, chocolate, blueberry와 객체에 있음을 참조하십시오. 그러나 사실 1002은 오직 chocolate입니다. 이

[{ 
"id": "0001", 
"type": "donut", 
"batterID": "1001", 
"batterType": "Regular" 
}, 
{ 
"id": "0001", 
"type": "donut", 
"batterID": "1002", 
"batterType": "Chocolate" 
}] 

귀하의 전문 지식을 평가처럼

내 이상적인 출력 될 것입니다!

EDIT 해결 : 작업 명령 : cat tmpData.txt | jq '[{id, type} + (.batter[] | {batterId: .id, batterType: .type})]'

답변

4
  1. "콤마없는"출력은 JSON 스트림이고; 배열을 내보내려면 jq 필터를 대괄호로 묶습니다.
  2. 넌 .batter [] 반복 카티 생성물을 생성하는 효과를 갖는다 {id: id, type: .type}
  3. {id, type}에 내 필터를 생략 할 수있다. 분명히 을 원하는 것은 .batter를 한 번만 확장하는 것입니다. 함께

퍼팅 모든 :

[{id, type} + (.batter[] | {batterId: .id, batterType: .type})] 
+0

나는 오류 '''-bash 점점 오전 : 예기치 않은 토큰 근처의 구문 오류 '('''을'이이 JQ 명령으로 보이는 방법을 보여 주시겠습니까? @peak – Goldfish

+0

jq 프로그램을 파일에 넣고, 예를 들어 program.jq를 실행하고 -f 옵션과 함께 jq를 호출하십시오. 예를 들어'jq -f program.jq tmpData.json' – peak

+0

작은 따옴표로 된 전체 JQ 명령 .. 편집을 참조하십시오 .. 감사합니다! – Goldfish

관련 문제