let say that the json object is > { "foo": "bar"}
after stringyfing i got > "{ \"foo\": \"bar\" }"
how can I get back the orginal json object using UPDATE sql query?
i'm aware of that it's a bad DB architecture it was designed by another engineer before me, that's why I would like get back the original json data and then alter the column to jsonb
Update:
please be aware that I'm looking for an answer to do that with only sql query and without any involving of programming languages like javascript.. etc
I was able to solve this same issue by doing some regexp replaces. You might not need the where clause, but in my case, I had a bug that started to stringify the JSONB column so only some of my data needed this change applied.
update your_table
set text =
jsonb(regexp_replace(regexp_replace(regexp_replace(text::text, '"$', ''), '^"+', ''), '\\"', '"', 'g'))
where text->>'foo' is null;
You could do to_json('{ \"foo\": \"bar\" }'::text)
so it would be something like
update yourtable set yourjsoncolumn = to_json(yourjsoncolum::text)
Related
I Currently have a column in json format with multiple items.
The struct is like the following:
phones": [{"phone": "11111111", "type": "CELLPHONE"}, {"phone":
"222222222", "type":"CELLPHONE"}, {"phone": "99999999", "type":
"CELLPHONE"}]
I tried:
json_extract(Contacts,'$.phones.phone') as phone_number but it only extracts the first one.
JSON_EXTRACT_ARRAY(Contacts,'$.phones') as phone_contacts gives me an array, But Im getting error trying to unnest it.
Does anybody know any approach to solve this problems?
Thanks in Advance.
Consider below
select json_value(phone_contact, '$.phone') phone,
json_value(phone_contact, '$.type') type
from your_table,
unnest(json_extract_array(Contacts,'$.phones')) phone_contact
if applied to sample data in your question - output is
I have json object in table column that contains array type key roles.
I am trying to replace roles value but it's instead replace existing roles, adding new inside roles.
here is the db fiddler.
current result:
{
"roles": {"roles":[{....}]}
}
expected result:
{
"roles":[{....}]
}
Any help is appriciated.
thanks.
Query look correct,But passing object {roles:[]} as input to JSON_QUERY(j.newJson) instead of array [] value.
Either you can modify input or extarct array value from input to get expected result by using JSON_QUERY path.
Before
JSON_MODIFY(fs1.[Schema],'$.roles',JSON_QUERY(j.newJson)) as [Schema]
After adding JSON_QUERY path
JSON_MODIFY(fs1.[Schema],'$.roles',JSON_QUERY(j.newJson,'$.roles')) as [Schema]
for more details see ths docs JSON_QUERY (Transact-SQL)
The point of my original answer was to fully rebuild the JSON, which is why I added , ROOT ('roles').
So you don't need to use JSON_MODIFY, just replace the entire column.
Alternatively, if for example you have other parts to the JSON, you can remove , ROOT ('roles') and leave JSON_MODIFY in.
I am having trouble working with the JSONB structure in PostgreSQL. currently my data is saved as follows:
"{\"Hello\":\"World\",\"idx\":0}"
Which obviously is not correct 😀 so I am trying to "repair" this and get the actual JSON representation for querying with:
SELECT regexp_replace(trim('"' FROM json_data::text), '\\"', '"', 'g')::jsonb FROM My_table
However when trying this, I get the following error:
ERROR: invalid input syntax for type json
DETAIL: Token "Рыба" is invalid.
CONTEXT: JSON data, line 1: ...х, как : Люди X, Пароль \\"Рыба...
SQL state: 22P02
So I am thinking that this is due to the character encoding that is not being accepted by the JSONB standard.
My main question then is though, how can I repair this kind of table so that I am still able to query it? I tried utilizing conver_from and convert_to but am unable to figure out how to fix this error... did anyone encounter this already?
Update: Found it! (thanks to Convert JSON string to JSONB), utilizing
SELECT (json_data#>>'{}')::jsonb FROM my_table
fixed it
Im am trying to save some special characters in a json column of a table. but this is actually not working
this is how it is stored
{"district":"\u099a\u09be\u0981\u09a6\u09aa\u09c1\u09b0",
"sub_district":"\u099a\u09be\u0981\u09a6\u09aa\u09c1\u09b0"}
i am storing my input values as below..i am using laravel
present = array('district'=>$request->district,'sub_district'=>$request->sub_districtce);
$card->present_address = json_encode($present);
and while searching for a string in that json object, i am using a query below
$allowances = Card::SELECT('id','name','nid','village')
->where(DB::raw("json_extract((present_address), '$.district')"), চাদপুর)
->get();
can anybody help me on this situation where i can store special characters/unicodes in that json object.?
It is perfectly normal. As soon as you will do json_decode(), you will get back your expected values:
array (
'district' => 'চাঁদপুর',
'sub_district' => 'চাঁদপুর',
)
I have been trying to figure out how to cleanly extract some_value from something that looks like this:
SELECT protobuff_text FROM table_name;
>>
resource_id {
high_part: 10310138280989442894
low_part: 12186277462739912197
}
...
}
known_field_name: some_value
mask {
points {
...
It should be extracted without assuming anything about what happens before or after it besides the newline. This is easy in MySQL... but I have been having a hard time figuring it out in postgres.
Any PostgreSQL experts out there who can help me with this?
P.S. The string that is inserted into the database is created via com.google.protobuf.TextFormat.printToString(Message m).
Try something like:
SELECT regexp_matches(protobuff_text,'resource_id\s*\{\s*high\_part\:\s*([0-9]*)\s*low\_part\:\s*([0-9]*)\s*\}')
FROM table_name;
This regex will get you the numeric values, you shown in the example.
For the known_field_name try something like:
SELECT regexp_matches(protobuff_text,'known_field_name\:\s*(\w*)')
FROM table_name;
Details here.