Add to JSON array - couchbase

How can I append a new element to the existing array using N1QL?
[
{
"address": "127.0.0.1",
"time": 0
},
{
"address": "127.0.0.1",
"time": 0
}
]

You can set to update the data
ARRAY_APPEND(field, { "adress":"127.0.0.1", "time":null })
where "key you need to use as reference' = "referemce data"

Related

Accessing list element in terraform output

I have following code in terraform state file( pasted just part of state file which I consider relevant for this question), which is result of running terraform code pasted below as well:
"mode": "managed",
"type": "azurerm_vpn_gateway",
"name": "azure_vpngw",
"provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"].azure_clusters",
"instances": [
{
"schema_version": 0,
"attributes": {
"bgp_settings": [
{
"asn": 65515,
"bgp_peering_address": "",
"instance_0_bgp_peering_address": [
{
"custom_ips": [
"169.254.21.1"
],
"default_ips": [
"10.255.176.12"
],
"ip_configuration_id": "Instance0",
"tunnel_ips": [
"10.255.176.4",
"20.184.79.231"
]
Relevant terraform code:
resource "azurerm_vpn_gateway" "azure_vpngw" {
provider = azurerm.azure_clusters
name = "azure_vpngw"
location = azurerm_resource_group.azure_networking.location
resource_group_name = azurerm_resource_group.azure_networking.name
virtual_hub_id = azurerm_virtual_hub.azure_hub.id
bgp_settings {
asn = 65515
peer_weight = 50
instance_0_bgp_peering_address {
custom_ips = ["169.254.21.1"]
}
instance_1_bgp_peering_address {
custom_ips = ["169.254.22.1"]
}
}
}
And I need to accesss last element in tunnel_ips list, so 20.184.79.231.
I have been trying diff things like
azurerm_vpn_gateway.azure_vpngw.bgp_settings[0].instance_0_bgp_peering_address[0].tunnel_ips[0][1]
but it did not work. So, issue is in last part - tunnel_ips[0][1] I believe. If anybody has idea, it would be very wellcome.
Since your code is not a valid TF code, I modified it to actually be such, and this is how you can access your ip:
locals {
t = <<EOL
{
"mode": "managed",
"type": "azurerm_vpn_gateway",
"name": "azure_vpngw",
"provider": "provider[azure_clusters",
"instances": [{
"schema_version": 0,
"attributes": {
"bgp_settings": [{
"asn": 65515,
"bgp_peering_address": "",
"instance_0_bgp_peering_address": [{
"custom_ips": [
"169.254.21.1"
],
"default_ips": [
"10.255.176.12"
],
"ip_configuration_id": "Instance0",
"tunnel_ips": [
"10.255.176.4",
"20.184.79.231"
]
}]
}]
}
}]
}
EOL
v = jsondecode(local.t)
}
output "test" {
value = local.v["instances"][0]["attributes"]["bgp_settings"][0]["instance_0_bgp_peering_address"][0]["tunnel_ips"][1]
}
gives:
test = "20.184.79.231"
I figure it out. So, to fetch IP and use it in another resource definition I did:
ip_address = sort(azurerm_vpn_gateway.azure_vpngw.bgp_settings[0].instance_0_bgp_peering_address[0].tunnel_ips)[1]

jmespath :select json object element based on other (array) element in the object

I have this JSON
{
"srv_config": [{
"name": "db1",
"servers": ["srv1", "srv2"],
"prop": [{"source":"aa"},"destination":"bb"},{"source":"cc"},"destination":"cc"},]
}, {
"name": "db2",
"servers": ["srv2", "srv2"],
"prop": [{"source":"dd"},"destination":"dd"},{"source":"ee"},"destination":"ee"},]
}
]
}
I try to build a JMESPath expression to select the prop application in each object in the main array, but based on the existence of a string in the servers element.
To select all props, I can do:
*.props [*]
But how do I add condition that says "select only if srv1 is in servers list"?
You can use the contains function in order to filter based on a array containing something.
Given the query:
*[?contains(servers, `srv1`)].prop | [][]
This gives us:
[
{
"source": "aa",
"destination": "bb"
},
{
"source": "cc",
"destination": "cc"
}
]
Please mind that I am also using a bit of flattening here.
All this run towards a corrected version of you JSON:
{
"srv_config":[
{
"name":"db1",
"servers":[
"srv1",
"srv2"
],
"prop":[
{
"source":"aa",
"destination":"bb"
},
{
"source":"cc",
"destination":"cc"
}
]
},
{
"name":"db2",
"servers":[
"srv2",
"srv2"
],
"prop":[
{
"source":"dd",
"destination":"dd"
},
{
"source":"ee",
"destination":"ee"
}
]
}
]
}

JSON transformation in node.js

I want to transform my data from one json structure to another. What is the best way to do it?
Here is my original resource (customer) structure is:
{
"id": "123",
"data": {
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
}
I want to change it to:
{
"id": "123",
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
Keeping in mind that I might have an array of resources(customers) being returned in GET /customers cases. I want to change that to an array of new data type.
If customer object is array of object then below will help you to get desire format result
var result = customerObj.map(x => {
return {
id: x.id,
name: x.data.name,
status: x.data.status,
contacts: x.data.contacts
};
});
here I have used Object.assign() it will be helpful to you
var arr={
"id": "123",
"data": {
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
}
arr=Object.assign(arr,arr.data);
delete arr['data'];
console.log(arr);
You have to Json.parse the json into variable, and then loop through the array of objects, changes the object to the new format, and then JSON.stringify the array back to json.
Example code
function formatter(oldFormat) {
Object.assign(oldFormat, oldFormat.data);
delete oldFormat.data;
}
let parsedData = JSON.parse(Oldjson);
//Take care for array of results or single result
if (parsedData instanceof Array) {
parsedData.map(customer => formtter(customer));
} else {
formatter(parsedData);
}
let newJson = JSON.stringify(parsedData);
console.log(newJson);
Edit
I made the formatter function cleaner by using Kalaiselvan A code

Sending the JSON response as array or normal object

I am implementing a restful service where I am getting the pdf names and their ids from the database in the JSON fromat. Which one of the both the convenient JSON resful service response?
First Option:
{
"results": {
"documentNames": [
"test.pdf",
"ireport-ultimate-guide.pdf",
"sending report.pdf",
"Motor Hour.pdf"
],
"documentds": [
21116,
21117,
21118,
21119
]
}
}
Second Option:
{
"results": {
"21116": "test.pdf",
"21117": "ireport-ultimate-guide.pdf",
"21118": "sending report.pdf",
"21119": "Motor Hour.pdf"
}
}
I would use this "third option": The result is a list of object.
{
"result": [{
"id": "21116",
"filename": "test.pdf"
},
{
"id": "21117",
"filename": "ireport-ultimate-guide.pdf"
},
{
"id": "21118",
"filename": "sending report.pdf"
},
{
"id": "21119",
"filename": "Motor Hour.pdf"
}
]
}
because it better models the object structure.
I would create an entity for each document that contains both name and I'd.
[
{"name": "doc_1", "id": 123},
{"name": "doc_2", "id": 456}
]

json object accessing

i know its very simple thing but i m stucked on it
i have json variable with data as follow
var jsonText =
'[ { "user": [ { "Gender": "M", "Minage": "19", "Maxage": "30", "MaritalStatusId":"0", }]
},
{ "user":[ { "maritialtype": "Does not matter" }]
},
{ "user": [ { "Value": "No" }]
} ]';
var jsonObject = JSON.parse(jsonText);
now i can access gender as jsonObject[0].user[0].Gender
but i'm not able to access maritialtype and Value
For maritialtype:
jsonObject[1].user[0].maritialtype
For Value:
jsonObject[2].user[0].Value
Because you have an array of three objects, user, which is an array or one object. It's kind of a weird structure.