Why this:
with j(jval) AS
(
select JSON_SERIALIZE('{"contract" :{"contract-type" : "P",
"contract-id" : 123451,
"proposal-no" : "123456",
"serial-no" : "serialasd922",
"product-code" : "s093",
"contract-sign-dt" : "2016-12-01T00:00:00",
"contract-start-dt" : "2016-12-09T00:00:00"
}
}' pretty) from dual
)
SELECT json_transform(jval, insert '$.contract[*].x' = '50' pretty)
FROM j
is not working? I’m Trying to insert new value under $.contract.
Set with insert on missing also not working, but when it comes to SET to update existing field – everything’s fine.
2
Answers
You are trying to insert
'$.contract[*].x'
, butcontract
is an object not an array, so the[*]
shouldn’t be there.Use
insert '$.contract.x' = 50
instead."contract" :
{
"contract-type" : "P",
"contract-id" : 123451,
"proposal-no" : "123456",
"serial-no" : "serialasd922",
"product-code" : "s093",
"contract-sign-dt" : "2016-12-01T00:00:00",
"contract-start-dt" : "2016-12-09T00:00:00",
"x" : "50"
}
}
fiddle
I’m not sure why
set
works with or without the[*]
.I don’t have an Oracle 19 server to test on but in Oracle 21, you want
$.contract.x
and not$.contract[*].x
:Which, for the sample data:
Outputs:
"contract" :
{
"contract-type" : "P",
"contract-id" : 123451,
"proposal-no" : "123456",
"serial-no" : "serialasd922",
"product-code" : "s093",
"contract-sign-dt" : "2016-12-01T00:00:00",
"contract-start-dt" : "2016-12-09T00:00:00",
"x" : "50"
}
}
To get
$.contract[*].x
to work thencontract
would need to be an array of objects that looked like{"contract" :[{},{}]}
and then after:The output would be:
"contract" :
[
{
"x" : "50"
},
{
"x" : "50"
}
]
}
fiddle