json 파일을 편집하려면 변수를 jq에 전달하세요.

json 파일을 편집하려면 변수를 jq에 전달하세요.

나는 jq에 변수를 전달하려고 시도하고 있으며 '.Linux.date.$var'지금까지는 훌륭하게 작동하는 이름으로 변수를 참조하려고 시도했습니다. 하지만 변수를 사용하여 호출하고 싶습니다.

나 이거 있는데 잘 작동해

exectime=$(date -d now);    
cp $check_exec_history $check_exec_history.tmp
    jq --arg key1 true --arg key2 "$exectime" --arg name "$name" '.Linux.script_executed.first = $key1 | .Linux.date_executed.first = $key2' $check_exec_history.tmp > $check_exec_history; 
    rm $check_exec_history.tmp;

이렇게 하고 싶지만 작동하지 않습니다.

name=first;
exectime=$(date -d now);
cp $check_exec_history $check_exec_history.tmp
jq --arg key1 true --arg key2 "$exectime" --arg name "$name" ".Linux.script_executed.$name = $key1 | .Linux.date_executed.$name = $key2" $check_exec_history.tmp > $check_exec_history; 
rm $check_exec_history.tmp;

지금까지 얻었습니다. 이 답변을 사용하여https://stackoverflow.com/q/40027395/9496100하지만 내가 어디서 잘못하고 있는지 잘 모르겠습니다.

name=first;
exectime=$(date -d now);    
cp $check_exec_history $check_exec_history.tmp
    jq --arg key1 true --arg key2 "$exectime" --arg name "$name" '.Linux.script_executed.name==$name = $key1 | .Linux.date_executed.name==$name = $key2' $check_exec_history.tmp > $check_exec_history; rm $check_exec_history.tmp;

답변1

jq의 모든 객체에 대해 대괄호 인덱싱을 사용할 수 있으므로 [$name]시도 중인 작업에 적합합니다.

jq --arg key1 true --arg name "$name" '.Linux.script_executed[$name] = $key1 ...' 

대괄호 사용은 잘 문서화되어 있지 않습니다.설명서에, 이는 에만 사용할 수 있는 것처럼 보이지만 .[xyz]표현식의 시작 부분이 아닌 한(예: 와 동일하지만 배열 생성자) ["x"]어디에서나 사용할 수 있습니다..x.a.x.a["x"]["x"]

사용에 주의하세요하나의위의 인용문 - 이 방법으로 Bash는 $name및 를 $key1쉘 변수로 해석하려고 시도하지 않습니다. 큰따옴표를 유지해야 합니다 --arg name "$name". 왜냐하면 실제로는 그렇습니다.쉘 변수는 안전하게 사용할 수 있도록 따옴표로 묶어야 합니다.

답변2

마이클 호머의 대답이 맞습니다. 운영상의 문제와 관련된 목표를 포스팅하겠습니다.

서버에 업데이트 jq하는 json 파일을 출력하기 위해 REST API 컬 호출을 통해 얻은 권한 맵을 수정하려고 합니다 . PUT다음은 컬 API 쿼리의 출력입니다.

{
  "revision": 2,
  "groups": {
    "1": {
      "1": {
        "native": "none",
        "schemas": "none"
      },
      "2": {
        "native": "write",
        "schemas": "all"
      }
    },
    "2": {
      "1": {
        "native": "write",
        "schemas": "all"
      },
      "2": {
        "native": "write",
        "schemas": "all"
      }
    }
  }
}

보시다시피 중첩되어 있습니다. 새로운 권한 집합을 생성하고 데이터베이스를 생성할 때마다 bash 변수를 사용하여 수정해 보았습니다. 예를 들어 groups."1"."2"."native""mode" 및 "mode" 값을 수정하려고 합니다 "groups."2"."2".native. 거칠긴 하지만 핵심은 이렇습니다groups.groupPermissionID.DatabaseID.*

Bash 쉘 스크립트를 통해 이 중첩 그래프를 동적으로 수정하기 위해 Michael Homer의 App 솔루션을 사용했습니다 [$name]. 제 경우에는 이 작업을 두 번 연속으로 해야 했습니다. 즉, [$gID][$dID]. 다음 설정에서 변수는 상수이지만 내 모델에서는 bash 쉘 스크립트에 전달된 명령 인수입니다.

dbID="2"
pgroupID="2"

curl -X GET -H "Content-Type: application/json" -H "X-Metabase-Session: XXXXX-XXXXX-XXXXX-XXXXXXXXX" "http://localhost:9000/api/permissions/graph" | jq --arg dID "$dbID"   --arg gID "$pgroupID" -r '.groups."1"[$dID]."native" = "none" | .groups."1" [$dID]."schemas" = "none" | .groups[$gID][$dID]."native" ="none" .groups[$gID][$dID]."schemas" ="none"' > permissiongraph.json

PUT내 서버로 보낼 다음과 같은 업데이트된 JSON 차트가 생성됩니다 .

{
  "revision": 2,
  "groups": {
    "1": {
      "1": {
        "native": "none",
        "schemas": "none"
      },
      "2": {
        "native": "none",
        "schemas": "none"
      }
    },
    "2": {
      "1": {
        "native": "write",
        "schemas": "all"
      },
      "2": {
        "native": "none",
        "schemas": "none"
      }
    }
  }
}

이 사건에 대한 기록이 거의 없다고 Michael이 말한 것은 옳습니다. 설명서에서 찾을 수 없습니다. 이것이 다른 사람에게 도움이 되기를 바랍니다.

관련 정보