如何使用PL/JSON创建新的JSON对象?

如何使用PL/JSON创建新的JSON对象?,json,plsql,pljson,Json,Plsql,Pljson,假设我想在PL/JSON中创建以下JSON结构: { "foo": "bar", "baz": 42, "foobar": [ 1, 2, 3 ] } 如何操作?最简单的方法是将字符串简单地传递给JSON类型构造函数,如下所示: declare json_obj json; begin json_obj := json('{"foo":"bar", "baz":42, "foobar": [1,2,3]}'); end; / 但让我们假设您正在从数据库中的数据构建这个JS

假设我想在PL/JSON中创建以下JSON结构:

{
  "foo": "bar",
  "baz": 42,
  "foobar": [ 1, 2, 3 ]
}

如何操作?

最简单的方法是将字符串简单地传递给JSON类型构造函数,如下所示:

declare
  json_obj json;
begin
  json_obj := json('{"foo":"bar", "baz":42, "foobar": [1,2,3]}');
end;
/
但让我们假设您正在从数据库中的数据构建这个JSON对象。然后您将执行以下操作:

declare
  json_obj json := json();
  json_ar  json_list := json_list();
begin
  -- Add the string elements
  json_obj.put('foo', 'bar');
  json_obj.put('baz', 42);

  -- Add elements to the array ("list" in PL/JSON)
  json_ar.append(1);
  json_ar.append(2);
  json_ar.append(3);
  -- Add the array to the object
  json_obj.put('foobar', json_ar);

  -- Print it just for fun
  json_obj.pretty_print
end;
/

最简单的方法是简单地将字符串传递给JSON类型构造函数,如下所示:

declare
  json_obj json;
begin
  json_obj := json('{"foo":"bar", "baz":42, "foobar": [1,2,3]}');
end;
/
但让我们假设您正在从数据库中的数据构建这个JSON对象。然后您将执行以下操作:

declare
  json_obj json := json();
  json_ar  json_list := json_list();
begin
  -- Add the string elements
  json_obj.put('foo', 'bar');
  json_obj.put('baz', 42);

  -- Add elements to the array ("list" in PL/JSON)
  json_ar.append(1);
  json_ar.append(2);
  json_ar.append(3);
  -- Add the array to the object
  json_obj.put('foobar', json_ar);

  -- Print it just for fun
  json_obj.pretty_print
end;
/

我们能把这个json对象转换成varchar2吗?是的,但这不是这个问题的主题。是的,但你能给我一个提示吗?它在文档中--。甚至有一个巨大的图表显示了您需要的所有方法。我们可以将这个json对象转换为varchar2吗?是的,但这不是这个问题的主题。是的,但您能给我一个提示吗?它在文档中--。甚至有一个伟大的大图显示了所有你需要的方法。