Skip to content

Instantly share code, notes, and snippets.

@iGarym
Created December 26, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iGarym/04fb89138a096e62ffafa75b780678ad to your computer and use it in GitHub Desktop.
Save iGarym/04fb89138a096e62ffafa75b780678ad to your computer and use it in GitHub Desktop.
JSON-pointers example

JSON-pointers

rules

  • 以 "/" 开头的为绝对路径,相对整个 json 元数据,否则为相对路径,以当前校验的字段为基准
  • "0": 当前 pointer 的 value
  • "0#": 当前 pointer 的 key
  • "1": 上一级 pointer 的 value
  • "1#": 上一级 pointer 的 key
  • "2": 上两级 pointer 的 value
  • "2#": 上两级 pointer 的 key
    ...以此类推

example

// json data
const json = {
  "isSunday": false,
  "user": {
    "name": "gary",
    "age": 25,
    "courses": [
      {
        "name": "physical",
        "score": 95,
      },
      {
        "name": "chemistry",
        "score": 90,
      }
    ],
  },
  "fruits": ["apple", "bananer"]
}
当前 pointer 相对路径 pointer 匹配结果
false "0" false
false "0#" "isSunday"
false "1" json
false "1#" <fail>
"gary" "0" "gary"
"gary" "0#" "name"
"gary" "1" json.user
"gary" "1#" "user"
"gary" "1/age" 25
"gary" "2" json
"gary" "2/isSunday" false
"gary" "2#" <fail>
"chemistry" "0" "chemistry"
"chemistry" "0#" "name"
"chemistry" "1" { "name": "chemistry", "score": 90 }
"chemistry" "1#" 1
"chemistry" "2" json.user.courses
"chemistry" "2#" "courses"
"chemistry" "3" json.user
"chemistry" "3#" "user"
"chemistry" "4" json
"chemistry" "3#" <fail>
"bananer" "0" "bananer"
"bananer" "0#" 1
"bananer" "1" json.list
"bananer" "1/0" "apple"
"bananer" "1#" "list"
"bananer" "2" "json"
"bananer" "2#" <fail>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment