Python 在特定模式后重命名JSON键

Python 在特定模式后重命名JSON键,python,awk,sed,Python,Awk,Sed,我有一个包含以下内容的JSON对象: { "quiz": { "sport": { "id": "77", "q1": { "question": "Which one is correct team name in NBA?",

我有一个包含以下内容的JSON对象:

{
    "quiz": {
        "sport": {
            "id": "77",
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "13"
                ],
                "answer": "13"
            },
        }
    }
}
我想找钥匙“id”。一旦找到,就开始寻找“答案”键,并将其重命名为“答案id”

预期产出:

{
    "quiz": {
        "sport": {
            "id": "77",
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Huston Rocket"
                ],
                "answer_id": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "13"
                ],
                "answer": "12"
            },
           
        }
    }
}
到目前为止,我已经尝试了这个方法,但没有正确地抓住场地:

awk   ' { for ( i = 1; i <= NF; ++i ) {

              if ( $i == "id" )
                  r = 1

              if ( r && $i == "answer")
                  $i = "answer_id" 
                  r = 0           
            }
          }
          1 ' example.json > example2.json

awk'{for(i=1;i这里,一个解决方案

data = {
    "quiz": {
        "sport": {
            "id": "77",
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "13"
                ],
                "answer": "13"
            },
        }
    }
}

for key in data.get('quiz'):
    inner_data = data['quiz'][key]
    if 'id' in inner_data and 'answer' in inner_data['q1']:
        inner_data['q1']['answer_id'] = inner_data['q1'].pop('answer')

print(data)


保留密钥顺序的jq解决方案:

walk(if type=="object" and .id
  then walk(if type=="object" and .answer 
    then  with_entries(if .key=="answer"
      then .key+="_id" else . end)
    else . end)
  else . end)
或者你想:

walk(if type=="object" and .id
  then .id |= walk(if type=="object" and .answer 
    then  with_entries(if .key=="answer"
      then .key+="_id" else . end)
    else . end)
  else . end)

对于相同的JSON查询,这里有一个基于
jtc
的解决方案(假设源JSON位于
file.JSON
):


bash$Hi!如果我能给你一个使用python解决这个问题的方法。它能帮你吗?是的!!@AndreiGurkoI刚刚发布了我的解决方案。请尝试一下))总是有一个问题,即
q1
还是会有多个问题?会有多个q1。我的意思是这是结构,但键会不止一次出现。是否有任何选项可以按照原始的顺序保存?@Max抱歉,但我不明白你的意思。你的意思是如何保存内部_数据['q1']的值。pop(‘答案’)或者别的什么?我的意思是,当我在虚拟机中执行代码时,它首先会给我“数学”而不是“运动”@Max,sanks作为解释。所以数据是一个dict结构,dict不能保证当前位置的所有键。但是从python3.7我读到,dict保证键的顺序。你会如何重命名标签“quick”“DATA1”你能解释一下吗?@马克斯-考虑{“ID”:0,“回答”:1 }对不起,我不明白。你在使用jq?@ max -请看你的Q的第一行,以及我的A的第一行。这对我的情况无效。有什么想法可以通过JQ来做吗?谢谢。advance@Max,jq解决方案由peak提供
bash $ <file.json jtc -w'<id>l:[-1]<answer>l:<>k' -u'"answer_id"'