Search and replace key's value in nested JSON - json

I have a json column with this format and How to modify element from postgres jsonb by key?
{
"Content": {
"CustomerInfo": {
},
"Identifier": null,
"Operating": {
"Identifier": "ABC-8585",
"TypeIdentifier": "VAL_OP",
"SaleIdentifier": "01000042"
},
},
}
How to remove "ABC-" from Identifier key?
Result In:
{
"Content": {
"CustomerInfo": {
},
"Identifier": null,
"Operating": {
"Identifier": "8585",
"TypeIdentifier": "VAL_OP",
"SaleIdentifier": "01000042"
},
},
}
Here is the query I apply to update a json value but it does not work.
update "tbleName" set "columnName" = replace('Content.Operating.Identifier','ABC-','')::jsonb ? 'Content.Operating.Identifier'

You can achieve this by using a combination of jsonb_set, regular JSON reading operators, and replace:
UPDATE "tbleName"
set "columnName" = jsonb_set(
"columnName"::jsonb,
'{Content,Operating,Identifier}',
replace(("columnName"::jsonb -> 'Content' -> 'Operating' -> 'Identifier')::text, 'ABC-', '')::jsonb
);
Here you compute the column's new value in the following way:
read the value of the desired nested JSON key
do the replacement
write back the result to the same nested JSON key in the original JSON structure

Related

Is it possible to add a key to a value that does not have a key in JSON?

I have a JSON without a key but just a value. Is it possible to add a key to it using React?
Here I have the and hanging without a key. How do I add a key to it, so that it's easier to read? I looked at JSON.Stringify and checked the replacer but it does not let me do it. delete also does not let me do it.
{
"sample": [
{
"id": "r-1",
"name": "sam"
},
"and",
{
"id": "r-2",
"name": "jerry"
}
]
}
You're confusing objects and arrays. The "key" with the string and as a value isn't a key. It's an array element (the value of the sample property is an array). It can't have a key unless you convert it from a string to an object, like the array elements before and after it, which are objects.
The structure of the array on the sample object property looks like this:
object | string | object
If you're ok changing your data from a string to an object, just replace the second array element (index 1) in the sample property with a new object:
const myObj = {
"sample": [{
"id": "r-1",
"name": "sam"
},
"and",
{
"id": "r-2",
"name": "jerry"
}
]
}
myObj.sample[1] = {
newKey: myObj.sample[1]
};
console.log(myObj);

how to remove [] character at extracted json using json path

{
"responseCode": "200",
"data": {
"sequence": 1,
"used": true,
"sensingTags": [
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
]
}
}
My goal is get updatedOn value from this json using jsonPath like this
1587557350251
i thought below jsonPath will work but it extract only empty list.
$..sensingTags[?(#.code == 'LED')][0].updatedOn
And i want to know how to extract value like below
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
Not like this one.
[
{
"code" : "LED",
"value" : 1,
"updatedOn" : 1587557350251
}
]
As per Getting a single value from a JSON object using JSONPath, JsonPath will always return an array (or a false) at that point...
Best you can do is process it as an array of updatedOn and simply always grab the first value.
$..sensingTags[?(#.code == 'LED')].updatedOn

updating Couchbase doc with value derived from key

I'm trying to update a Couchbase doc from one structure to another
here is the current structure
{
"config": {
"160x600": {
"siteId": "123455677"
},
"300x250": {
"siteId": "123455677"
},
"300x600": {
"siteId": "123455677"
}
}
}
Desired structure is
{
"config": {
"160x600": {
"siteId": "123455677",
"size":[160,600]
},
"300x250": {
"siteId": "123455677",
"size" : [300,250]
},
"300x600": {
"siteId": "123455677",
"size": [300,600]
}
}
}
Basically I wants to iterate over the keys inside config, split each key on 'x' and assign the resulting array as a value to "size" inside config[key].
Here is an N1QL query I tried (which obviously didn't work, hence this question here);
update AppBucket a set a.config[`size`].size = split(size, `x`) for size in OBJECT_NAMES(a.config) end
where meta(a).id like 'cnfg::40792';
Thanks in advance.
If you need to access the dynamic field you must use array brackets immediately after dot (map access) i.e config.[v] and v must be string or evaluate to string. It evaluates expression inside array brackets, it must be string, then converts into identifier and substitutes it and evaluate rest of the path. ex: config.["xyz"] ==> config.xyz , config.[f1] (f1 is "field1") ===> config.field1
Also split key must be string not identifier i.e. "x"
INSERT INTO default VALUES ("cnfg::40792", { "config": { "160x600": { "siteId": "123455677", "size":[160,600] }, "300x250": { "siteId": "123455677", "size" : [300,250] }, "300x600": { "siteId": "123455677", "size": [300,600] } } });
UPDATE default AS a USE KEYS "cnfg::40792"
SET a.config.[v].size = SPLIT(v, "x") FOR v IN OBJECT_NAMES(a.config) END;
The updated ARRAY will have strings i.e ["160","600"]. If you need numbers use the following command. If string can't convert to number it will converts to NULL.
UPDATE default AS a USE KEYS "cnfg::40792"
SET a.config.[v].size = ARRAY TO_NUMBER(v1) FOR v1 IN SPLIT(v, "x") END
FOR v IN OBJECT_NAMES(a.config) END;

How to query to JSON column in a postgres table , to retrieve all the values from all json for a particular json key

We are using postgres , in that we had a table containing column of type of JSON , in that we have below type of JSON documents ,
please find sample here
i want all the values of json key student_id from all the JSON docum
i mean we have such documents , for each row for that column and each document contains contains that JSON key , am trying to get all the values for that json key from all the documents(all rows for that column)
You are probably looking for json_array_elements.
Find everything you need in the json functions page.
For your sample you could do something like this:
select json_array_elements(json_array_elements('{
"srs_student_information": {
"header": {
"name": "kkkk"
},
"beginning_segment": {
"age": 12
},
"loop_id_sls": [{
"student_level_details": {
"class": "12"
},
"parent_details": [{
"name": "assa"
}],
"student_identification": [{
"student_id_qual": "BM",
"student_id": "00547311"
}, {
"student_id_qual": "CN",
"student_id": "467931496024"
}, {
"student_id_qual": "CN",
"student_id": "467931496035"
}, {
"student_id_qual": "CN",
"student_id": "467931496046"
}]
}]
}
}'::json->'srs_student_information'->'loop_id_sls')->'student_identification')->>'student_id' student_id

Nested JSON query in postgres

I have some JSON along the following lines, the format of which cannot, unfortunately, be changed:
{
"elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
This is stored in a postgres database and I'd like to pull out records by means of the id embedded in the JSON above. So far I've tried stuff like this:
SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';
...without success; although this query runs it returns nothing. Can anyone suggest how this might be done?
Create schema:
create table json (
id serial primary key,
data jsonb
);
insert into json (data) values (
'{ "elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
'::jsonb);
Query itself:
select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node
where node->'data'->>'id' = 'n0';