问题在oracle 12c的JSON_VALUE中使用JSON来处理超过11000个字符

时间:2017-01-04 21:11:04

标签: sql json oracle plsql oracle12c

我在Oracle 12c上运行以下代码:

DECLARE
       l_json clob;
       l_var varchar2(90);
       l_query clob;
       mypath clob;
       l_path_value varchar2(100);
BEGIN
    SELECT json_column into l_json from my_table;
    SELECT path_column into l_path_value from my_table;  -- this returns 'watchers'
    mypath := '$.second."'||l_path_value||'"'; -- $.second."watchers"
    l_query := 'select json_value('''||l_json||''', '''||mypath||''' ERROR ON ERROR)  from dual';
    execute Immediate l_query into l_var;
    DBMS_OUTPUT.PUT_LINE('var: '||l_var);
END;
  

运行之后,我得到的结果如下:
  错误报告 -
  ORA-40441:JSON语法错误
  ORA-06512:第12行   40441. 00000 - " JSON语法错误"
  *原因:提供的JavaScript Object Notation(JSON)数据无效              语法,无法解析   *操作:使用正确的语法提供JSON数据。

我期待结果如下:

  

var:0

那么JSON的长度是否存在问题?

我的json看起来像:

  {
"first": {
    "private": false,
    "html_url": "https://github.com/mralexgray/Alfred-Google-Translate",
    "description": "Extension for Alfred that will do a Google translate for you",
    "fork": true,
    "url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate",
    "forks_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/forks",
    "keys_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/teams",
    "hooks_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/hooks",
    "issue_events_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues/events{/number}",
    "events_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/events",
    "assignees_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/assignees{/user}",
    "branches_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/branches{/branch}",
    "tags_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/tags",
    "blobs_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/languages",
    "stargazers_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/stargazers",
    "contributors_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/contributors",
    "subscribers_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/subscribers",
    "subscription_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/subscription",
    "commits_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/contents/{+path}",
    "compare_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/merges",
    "archive_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/downloads",
    "issues_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues{/number}",
    "pulls_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/labels{/name}",
    "releases_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/releases{/id}",
    "deployments_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/deployments",
    "created_at": "2013-06-04T10:45:10Z",
    "updated_at": "2013-06-04T10:45:10Z",
    "pushed_at": "2013-01-12T19:39:03Z",
    "git_url": "git://github.com/mralexgray/Alfred-Google-Translate.git",
    "ssh_url": "git@github.com:mralexgray/Alfred-Google-Translate.git",
    "clone_url": "https://github.com/mralexgray/Alfred-Google-Translate.git",
    "svn_url": "https://github.com/mralexgray/Alfred-Google-Translate",
    "homepage": null,
    "size": 103,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "Shell",
    "has_issues": false,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "open_issues_count": 0,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "master"
},
"second": {
    "private": false,
    "html_url": "https://github.com/mralexgray/Alfred-Google-Translate",
    "description": "Extension for Alfred that will do a Google translate for you",
    "fork": true,
    "url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate",
    "forks_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/forks",
    "keys_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/teams",
    "hooks_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/hooks",
    "issue_events_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues/events{/number}",
    "events_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/events",
    "assignees_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/assignees{/user}",
    "branches_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/branches{/branch}",
    "tags_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/tags",
    "blobs_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/languages",
    "stargazers_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/stargazers",
    "contributors_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/contributors",
    "subscribers_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/subscribers",
    "subscription_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/subscription",
    "commits_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/contents/{+path}",
    "compare_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/merges",
    "archive_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/downloads",
    "issues_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/issues{/number}",
    "pulls_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/labels{/name}",
    "releases_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/releases{/id}",
    "deployments_url": "https://api.github.com/repos/mralexgray/Alfred-Google-Translate/deployments",
    "created_at": "2013-06-04T10:45:10Z",
    "updated_at": "2013-06-04T10:45:10Z",
    "pushed_at": "2013-01-12T19:39:03Z",
    "git_url": "git://github.com/mralexgray/Alfred-Google-Translate.git",
    "ssh_url": "git@github.com:mralexgray/Alfred-Google-Translate.git",
    "clone_url": "https://github.com/mralexgray/Alfred-Google-Translate.git",
    "svn_url": "https://github.com/mralexgray/Alfred-Google-Translate",
    "homepage": null,
    "size": 103,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "Shell",
    "has_issues": false,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "open_issues_count": 0,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "master"
   }
}

2 个答案:

答案 0 :(得分:0)

如果你的varchar2(90)对于返回值来说太小,它可能会返回null。请参阅JSON_value_on_error_clause以获取参考。

答案 1 :(得分:0)

我不确定它会有所帮助 - 您已经证明它没有任何区别的示例,我不确定是否有任何控件或多字节字符在不同的值可能会导致问题 - 但我会绑定JSON值,而不是隐式转换为字符串并返回:

l_query := 'select json_value(:json, '''||mypath||''' ERROR ON ERROR)  from dual';
execute Immediate l_query into l_var using l_json;