英文:
How to update JSON value in Oracle by adding over there value from another column?
问题
以下是您提供的代码的翻译部分:
DECLARE
  v_my_json VARCHAR2(4000);
  v_type NUMBER;
BEGIN
  FOR rec IN (
    SELECT *
    FROM database.table t
    WHERE MY_JSON LIKE '{
    "common": [{
        "code": ["FIRST_CODE"]
    }]'
  )
  LOOP
    SELECT TYPE_ID
    INTO v_type
    FROM database.table
    WHERE ID = rec.ID;
    v_my_json := REPLACE(rec.MY_JSON,
        '{
    "common": [{
        "code": ["FIRST_CODE"]
    }]',
        '{
    "common": [{
        "code": ["FIRST_CODE"], 
        "type": ' || v_type || '}]');
    UPDATE database.table
    SET MY_JSON = v_my_json
    WHERE ID = rec.ID;
  END LOOP;
END;
/
注意:这段代码似乎用于在Oracle数据库中更新JSON数据。如果您有任何进一步的问题或需要帮助,请随时提出。
英文:
I would like to update values in JSON in my table in Oracle which look like (same values for about 80 rows):
{
    "common": [{
        "code": ["FIRST_CODE"]
    }]
}
and make it looks like this:
{
    "common": [{
        "code": ["FIRST_CODE"],
        "type": 1
    }]
}
I want to get type from column TYPE_ID from the same table (it contains only 1 and 2 values).
Can you help me please - how can I do it correctly? Because now I've written the following script and it works but it did not change my table:
DECLARE
  v_my_json VARCHAR2(4000);
  v_type NUMBER;
BEGIN
  FOR rec IN (
    SELECT *
    FROM database.table t
    WHERE MY_JSON LIKE '{
    "common": [{
        "code": ["FIRST_CODE"]
    }]
}'
  )
  LOOP
    SELECT TYPE_ID
    INTO v_type
    FROM database.table
    WHERE ID = rec.ID;
    v_my_json := REPLACE(rec.MY_JSON,
        '{
    "common": [{
        "code": ["FIRST_CODE"]
    }]
}',
        '{
    "common": [{
        "code": ["FIRST_CODE"], 
        "type": ' || v_type || '}]');
    UPDATE database.table
    SET MY_JSON = v_my_json
    WHERE ID = rec.ID;
  END LOOP;
END;
/
答案1
得分: 0
请使用 JSON 函数而不是字符串函数来处理 JSON 数据,具体可以使用 JSON_TRANSFORM 函数。以下是您提供的代码示例:
with sample(json_val, other_col) as (
  select '{
    "common": [{
        "code": ["FIRST_CODE"]
    }, {
    "code": ["ANOTHER_ARRAY_ITEM"]
  }]
}', 1
  from dual
)
select
  json_val,
  json_transform(
    json_val,
    insert '$.common[*].type' = other_col
  ) as new_val
from sample
此查询将在 json_val 的 JSON 数据中插入一个 type 字段,其值为 other_col,并返回新的 JSON 数据。表格中包含了原始数据和处理后的结果。
您还可以在 fiddle 上查看示例。
英文:
Don't use string functions to deal with JSON. Use JSON functions instead: JSON_TRANSFORM.
with sample(json_val, other_col) as (
  select '{
    "common": [{
        "code": ["FIRST_CODE"]
    }, {
    "code": ["ANOTHER_ARRAY_ITEM"]
  }]
}', 1
  from dual
)
select
  json_val,
  json_transform(
    json_val,
    insert '$.common[*].type' = other_col
  ) as new_val
from sample
| JSON_VAL | NEW_VAL | 
|---|---|
| {<br> "common": [{<br> "code": ["FIRST_CODE"]<br> }, {<br> "code": ["ANOTHER_ARRAY_ITEM"]<br> }]<br>} | {"common":[{"code":["FIRST_CODE"],"type":1},{"code":["ANOTHER_ARRAY_ITEM"],"type":1}]} | 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论