试图将json数组转换为字段问题

时间:2018-05-18 22:23:22

标签: json debugging jq

我有以下JSON解析OK: 这只是我要发送的一些术语代码数据。

[{
"TermCode": 2164,
"ACAD_LEVEL_BOT": "GR",
"ACAD_LEVEL_EOT": "GR",
"ACAD_CAREER": "GRAD",
"UA_PRIM_MAJ_PLN": "DPMDP",
"UA_DEGR_DT": "1900-01-01",
"UA_PRIM_MJ_PLN_DES": "Development Practice",
"UA_PRIM_MIN_PLN_D": "-",
"CUM_GPA": 3.707,
"CUR_GPA": 4,
"TOT_CUMULATIVE": 300,
"UNT_PASSD_PRGRSS": 10,
"UNT_TAKEN_PRGRSS": 10,
"UA_PRIM_MJ_PLN_OWN": "School of Anthropology",
"ADMIT_TYPE": "GRD",
"ADMIT_TERM": "2144",
"UA_FA_ST_RES_DESCR": "Resident",
"VISA_PERMIT_TYPE": "-",
"UA_SALT_ROSTER": "-",
"UA_SALT_STATUS": "-",
"DEGR_CHKOUT_STAT": "-",
"EXP_GRAD_TERM": "-",
"UNT_TRNSFR": 0,
"ACAD_PROG_LD": "Graduate Degree Seeking",
"TOT_TRNSFR": 0,
"TUITION_RES": "RES",
"UA_CITIZEN_COUNTRY": "United States",
"UA_CITIZEN_ST_DESC": "Citizen",
"ACADEMIC_LOAD": "F",
"UNT_AUDIT": 0,
"CAMPUS": "MAIN",
"ACADEMIC_YEAR": "2016-2017",
"UA_DEGREE_LEVEL": "Masters",
"ACAD_PROG": "GDEG",
"UA_SEC_MAJ_PLN": "-",
"ACAD_ORG": "0410",
"EFF_START_DT": "2016-12-18 20:37:08",
"CURRENT_IND": "N",
"PROG_STATUS": "AC",
"PROG_ACTION": "MATR"

}]

我想将此数组用作名为term data的字段,因此我尝试:

[{
termdata: [{
    "TermCode": 2164,
    "ACAD_LEVEL_BOT": "GR",
    "ACAD_LEVEL_EOT": "GR",
    "ACAD_CAREER": "GRAD",
    "UA_PRIM_MAJ_PLN": "DPMDP",
    "UA_DEGR_DT": "1900-01-01",
    "UA_PRIM_MJ_PLN_DES": "Development Practice",
    "UA_PRIM_MIN_PLN_D": "-",
    "CUM_GPA": 3.707,
    "CUR_GPA": 4,
    "TOT_CUMULATIVE": 300,
    "UNT_PASSD_PRGRSS": 10,
    "UNT_TAKEN_PRGRSS": 10,
    "UA_PRIM_MJ_PLN_OWN": "School of Anthropology",
    "ADMIT_TYPE": "GRD",
    "ADMIT_TERM": "2144",
    "UA_FA_ST_RES_DESCR": "Resident",
    "VISA_PERMIT_TYPE": "-",
    "UA_SALT_ROSTER": "-",
    "UA_SALT_STATUS": "-",
    "DEGR_CHKOUT_STAT": "-",
    "EXP_GRAD_TERM": "-",
    "UNT_TRNSFR": 0,
    "ACAD_PROG_LD": "Graduate Degree Seeking",
    "TOT_TRNSFR": 0,
    "TUITION_RES": "RES",
    "UA_CITIZEN_COUNTRY": "United States",
    "UA_CITIZEN_ST_DESC": "Citizen",
    "ACADEMIC_LOAD": "F",
    "UNT_AUDIT": 0,
    "CAMPUS": "MAIN",
    "ACADEMIC_YEAR": "2016-2017",
    "UA_DEGREE_LEVEL": "Masters",
    "ACAD_PROG": "GDEG",
    "UA_SEC_MAJ_PLN": "-",
    "ACAD_ORG": "0410",
    "EFF_START_DT": "2016-12-18 20:37:08",
    "CURRENT_IND": "N",
    "PROG_STATUS": "AC",
    "PROG_ACTION": "MATR"
}]      }]

但是,这并没有解析。我的语法有什么问题? Stack Overflow中有一个错误,不允许我在这里结束我的问题所以我必须添加更多描述请向下箭头我没有选择。所以我的下一步是让json如下: [{termdata:jsonarray,coursedata:jsonarray,admitdata:jsonarray}] 任何有关这方面的想法也会受到赞赏。

1 个答案:

答案 0 :(得分:0)

要调试您的小代码段,可以使用https://jsonlint.com, 它提供了有用的错误消息,可以轻松查明问题。

作为也适用于大型文件的替代方案,您可能需要考虑jq。将您的代码段放在文件" input.txt"中,调用:

jq empty input.txt

产生错误消息:

  

解析错误:第2行第9列的文字无效

这相当于说第一把钥匙没有被正确引用。

(这里,empty具有抑制正常输出的效果。)

让jq做步行

更好的是,通过让jq完成工作,你可以省去麻烦:

jq '[{termdata:.}]' original.json

这将产生所需的输出。除非特别指示,否则jq将使用JSON(或JSON流)并生成JSON(或JSON流)。

您还可以使用jq对多个输入文件进行更复杂的转换。