
명확하게 말하자면, 여기에 있는 또 다른 정적 JSON 파일에 기록될 의사 JSON 출력을 원합니다. 따라서 배열이나 다른 것으로 래핑되지 않고 단순히 출력의 각 항목 뒤에 누락된 쉼표를 가져옵니다.
현재 내 쿼리는 다음과 같습니다.
.[] | select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
출력은 다음과 같습니다.
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
실제로 내가 원하는 경우:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
},
각 항목 뒤에 쉼표를 적어두세요! 그것은 나를 미치게 만들었습니다. 나는 다양한 위치에 을 추가하려고 시도했지만 join(", ")
최종 출력에 아무 것도 하지 않거나 배치한 위치에 따라 컴파일에 실패합니다.
여기에 데이터가 포함된 jqplay가 있습니다.https://jqplay.org/s/xx3F_IWn03g
원래 입력 JSON:
[
{
"name": "minScore",
"expected": 0.8,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "performance",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "accessibility",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 0.96,
"values": [
0.93,
0.96,
0.96
],
"operator": ">=",
"passed": true,
"auditProperty": "best-practices",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.5,
"actual": 0.64,
"values": [
0.64,
0.64,
0.64
],
"operator": ">=",
"passed": true,
"auditProperty": "seo",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
}
]
답변1
당신은 싶어 보인다수정하다auditId
문자열로 설정된 항목입니다 categories
. 당신이 하고 있는 일은발췌최상위 배열의 해당 요소를 수정합니다. 이는 객체 세트(배열이 아님)를 제공합니다.
|
배열에서 요소를 추출하는 데 사용하는 대신 다음을 사용하십시오.|=
해당 요소를 수정합니다. 결과 배열을 다른 파일의 JSON 구조에 올바르게 삽입하는 방법은 이 답변의 끝을 참조하세요.
그래서, 사용
.[] |= (
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
.[]
또는 전혀 사용하지 말고 대신 사용하십시오.map()
최상위 배열의 각 요소에 대한 표현식을 사용합니다.
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
이러한 표현식은 다음과 같은 요소 배열을 제공합니다.
[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
]
이 배열을 다른 파일의 기존 배열에 추가하려면 다음과 같이 하면 됩니다. ( array
최상위 수준에 키가 있고 otherfile
입력이 에 있다고 가정합니다 inputfile
. 키가 아직 없으면 생성됩니다.)
jq '.array += ( input |
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
)' otherfile inputfile
이 실행 되면 input
명령줄에 있는 두 번째 파일의 배열을 읽습니다.
기존 키 값을 추가하는 대신 교체하려면 +=
just로 변경하세요 .=
array
답변2
예, 그것이 []
반복자가 하는 일입니다. 각각에 대해 하나의 JSON 파일을 얻습니다. 배열을 얻으려면 전체 표현식을 묶어서 [...]
배열을 만들거나 해당 요소를 반복하는 map()
대신 입력 배열을 수정하는 데 사용할 수 있습니다.[]
$ echo '[1,2,3,4]' | jq '.[]|select(.<3)'
1
2
$ echo '[1,2,3,4]' | jq '[.[]|select(.<3)]'
[
1,
2
]
$ echo '[1,2,3,4]' | jq 'map(select(.<3))'
[
1,
2
]
둘러싸는 요소 없이 쉼표로 구분된 요소를 사용하여 [
배열 ]
로 만들면 json이 유효하지 않게 됩니다. 하지만 원하는 경우 파이프로 연결하여 sed '1d;$d'
첫 번째 줄과 마지막 줄을 삭제할 수 있습니다.