格式不正确的json文件导致我的终端出现问题

时间:2019-04-23 14:07:33

标签: json bash bash-it

我在搞乱我不应该使用的文件,以便编辑我的bash shell使其看起来不错。我下载了bash_it,并按照本教程进行操作(https://medium.freecodecamp.org/jazz-up-your-bash-terminal-a-step-by-step-guide-with-pictures-80267554cb22)。但是,现在我在shell中看到的就是每一行:

Expecting ',' delimiter: line 39 column 2 (char 2196)
Expecting ',' delimiter: line 39 column 2 (char 2196)

然后我去在文件的末尾添加一个逗号,我认为这是问题的根源(default.json),看起来像这样:

{
    "name": "Default color scheme for shell prompts",
    "groups": {
        "hostname":         { "fg": "brightyellow", "bg": "mediumorange", "attrs": [] },
        "environment":      { "fg": "white", "bg": "darkestgreen", "attrs": [] },
        "mode":             { "fg": "darkestgreen", "bg": "brightgreen", "attrs": ["bold"] },
        "attached_clients": { "fg": "white", "bg": "darkestgreen", "attrs": [] }
    },
    "mode_translations": {
        "vicmd": {
            "groups": {
        "hostname":         { "fg": "brightyellow", "bg": "mediumorange", "attrs": [] },
        "environment":      { "fg": "white", "bg": "darkestgreen", "attrs": [] },
        "mode":             { "fg": "darkestgreen", "bg": "brightgreen", "attrs": ["bold"] },
        "attached_clients": { "fg": "white", "bg": "darkestgreen", "attrs": [] },

        "gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
            "gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
            "gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
            "gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
            "gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
            "gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
            "gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
            "gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
            "gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
            "gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
            "gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }
        },  
        "mode_translations": {
            "vicmd": {
                "groups": {
                    "mode": {"fg": "darkestcyan", "bg": "white", "attrs": ["bold"]}
            }
        }
    }
},

然后我又在终端弹出另一个错误

Expecting property name enclosed in double quotes: line 39 column 3 (char 2197)
Expecting property name enclosed in double quotes: line 39 column 3 (char 2197)

所以我加了双引号,它期望另一个逗号,更多的引号,等等。

理想情况下,我只是希望能够再次使用我的终端,而不会在每一行出现这些json错误。

1 个答案:

答案 0 :(得分:1)

在线上确实有很多JSON验证器,例如:https://jsoncompare.com/#!/simple/

但是,我使用自己的离线Unix工具 jtc ,使用调试选项(-d),很容易在JSON中找出问题所在的位置(因此也可以将该工具用作JSON验证程序)。在您的情况下,在JSON末尾添加逗号是错误的,因为通常必须使用}]关闭任何嵌套的JSON。因此,删除尾随逗号后,该工具将提供以下输出:

bash $ <default.json jtc -d
.read_inputs(), reading json from <stdin>
.parsejson(), exception locus: ...          }|        }|    }|}|
.location_(), exception spot: --------------------------------->| (offset: 2421)
jtc json exception: unexpected_end_of_string
bash $ 
JSON末尾的

unexpected_end_of_string可能仅意味着一件事:缺少括号]}。在您的JSON中,没有丢失的未关闭数组(它们都关闭了[]),因此仅丢失了}的关闭大括号。

最后一个接一个地添加,直到JSON开始解析为止(总共添加了2个):

bash $ <default.json jtc -d
.read_inputs(), reading json from <stdin>
.write_json(), outputting json to <stdout>
{
   "groups": {
      "attached_clients": {
         "attrs": [],
         "bg": "darkestgreen",
         "fg": "white"
      },
      "environment": {
         "attrs": [],
         "bg": "darkestgreen",
         "fg": "white"
      },
      "hostname": {
...