I have a file that's quite large with entries that look like this:
{
"_id": {
"$oid": "572a5b93ae5174d3c4177da3"
},
"email": "removed#gmail.com",
"gender": "F",
"zip": "32934",
"state": "FL",
"city": "EAU GALLIE",
"address1": "removed",
"last_name": "removed",
"first_name": "removed",
"updatedAt": {
"$date": "2016-05-04T20:29:02.061Z"
},
"__v": 0,
"createdAt": {
"$date": "2016-05-04T20:28:54.948Z"
}
}
{
"_id": {
"$oid": "57a49bed913aebc7257145b9"
},
"email": "removed#gmail.com",
"dob": "11/06/1996",
"gender": "F",
"zip": "SN14 8BZ",
"address1": "removed",
"last_name": "removed",
"first_name": "removed",
"updatedAt": {
"$date": "2016-08-16T23:53:30.161Z"
},
"__v": 0,
"createdAt": {
"$date": "2016-08-05T14:00:13.130Z"
}
}
{
"_id": {
"$oid": "57a49bed913aebc7257145d3"
},
"email": "removed#netzero.net",
"zip": "NULL",
"state": "NULL",
"city": "NULL",
"address1": "NULL",
"last_name": "removed",
"first_name": "removed",
"updatedAt": {
"$date": "2016-08-05T14:00:13.467Z"
},
"__v": 0,
"createdAt": {
"$date": "2016-08-05T14:00:13.467Z"
}
}
{
"_id": {
"$oid": "57ab71379f7474b50eef976d"
},
"updatedAt": {
"$date": "2016-08-16T23:40:55.851Z"
},
"createdAt": {
"$date": "2016-08-10T18:23:51.177Z"
},
"email": "removed#hotmail.co.uk",
"ip": "0.0.0.0",
"first_name": "removed",
"last_name": "removed",
"address1": "removed",
"city": "",
"state": "",
"zip": "removed",
"gender": "F",
"__v": 0,
"dob": "03/01/1973"
}
{
"_id": {
"$oid": "57ab7137913aebc725194a20"
},
"email": "removed#gmail.com",
"job": "DeliveryDriver",
"zip": "24401",
"state": "VA",
"city": "FISHERSVILLE",
"updatedAt": {
"$date": "2016-09-16T12:45:50.984Z"
},
"__v": 0,
"createdAt": {
"$date": "2016-08-10T18:23:50.813Z"
},
"gender": "M",
"last_name": "removed",
"first_name": "removed"
}
and it's not in a particular order, i obviously removed names, address,ip's, and emails for privacy concerns. But the lines are all over, more than 20M of them.
How can i parse this properly? I'm looking to only extract Email, IP, Phone number, Name (First and Last) and Address (Zip, Address1,Addres2, City)
Some of these lines only have email & IP, and some have Email, IP, name, and some have Email, Name, Address and so on, including some with all the lines (they all have some junk data like OID, created and updated date, gender, ect)
What would the best way parsing this? I've been trying for a while now and i know it's been done, Thank you!
Don't try to parse json with regex, instead, try jq.
It's cross-platforms.
Example, adapt the command to your needs :
$ jq '(.email, .first_name, .last_name)' file.json
Output:
"removed#gmail.com"
"removed"
"removed"
"removed#gmail.com"
"removed"
"removed"
"removed#netzero.net"
"removed"
"removed"
"removed#hotmail.co.uk"
"removed"
"removed"
"removed#gmail.com"
"removed"
"removed"
Check https://stedolan.github.io/jq/
Or you can use nodejs and js code
Related
I just need to query all information of the doctor in the first index.
Here is my sample XML data
Here is my sample JSON data:
Both XML and JSON data are same. I just converted the XML to JSON format.
I have more documents like this.
I tried this query in this image
this query showing all information but I just need first doctors information.
I also tried in command prompt.Here is the query done in cmd
Here is my sample JSON code block: but I have more similar documents
{
"doctors": {
"doctor":[
{
"ssn": "257-79-xxxx",
"name": "Mavis Bxx",
"address": "xxxx Rusk Drive",
"country": "France",
"email": "",
"phone": "",
"patients": {
"patient": [
{
"gender": "Male",
"name": "Itch xxxx",
"address": "xx Cottonwood Avenue",
"revenue": "254",
"_country": "Spain",
"_id": "27"
},
{
"gender": "Male",
"name": "Damon xxxxx",
"address": "xx David Trail",
"revenue": "370",
"_country": "Germany",
"_id": "21"
}
]
},
"_id": "6"
},
{
"ssn": "179-45-xxxx",
"name": "Tobie Conxxxx",
"address": "x Comanche Center",
"country": "Spain",
"email": "tconringh#xxx.xxx",
"phone": "+86 998 262 xxxx",
"patients": {
"patient": {
"gender": "Male",
"name": "Vergil Tome",
"address": "x Melody Drive",
"revenue": "254",
"_country": "Germany",
"_id": "15"
}
},
"_id": "18"
},
{
"ssn": "777-59-xxxx",
"name": "Gertrud Macxxxx",
"address": "x Buell Drive",
"country": "USA",
"email": "gmaclaig2#xxx.com",
"phone": "+62 975 394 xxxx",
"patients": {
"patient": [
{
"gender": "Non-binary",
"name": "Dre Skxxxx",
"address": "x Becker Circle",
"revenue": "400",
"_country": "Germany",
"_id": "20"
},
{
"gender": "Female",
"name": "Arleyne Lestxxxx",
"address": "xx Farragut Court",
"revenue": "225",
"_country": "France",
"_id": "22"
}
]
},
"_id": "3"
}
You needs to change your collect data format. Your current format is single big size data. Not a quarriable format.
So
From current format
{
"doctors": {
"docker": [
{
...
"patients": {
"patient": [
{
...
},
{
...
}
]
},
},
{
...
}
]
}
}
To this format
[
{
...
"patients": [
{
...
},
{
...
}
]
},
{
...
}
]
Test JSON
[
{
"ssn": "2xx-7x-4xxx",
"name": "Mavis Bxxxed",
"address": "9xxx Rusk Drive",
"country": "France",
"email": "",
"phone": "",
"patients": [
{
"gender": "Male",
"name": "Itch Txxx",
"address": "5xx Cottonwood Avenue",
"revenue": "25x",
"_country": "Spain",
"_id": "27"
},
{
"gender": "Male",
"name": "Damon Wxxx",
"address": "09xx Dxxxx Trail",
"revenue": "370",
"_country": "Germany",
"_id": "21"
}
],
"_id": "6"
},
{
"ssn": "19-45-xxxx",
"name": "Tobie Coxxxx",
"address": "8x Comxxxx Center",
"country": "Spain",
"email": "tconxxxxx#usa.gov",
"phone": "",
"patients": [
{
"gender": "Male",
"name": "Some one",
"address": "1 Mexxxx Drive",
"revenue": "254",
"_country": "Germany",
"_id": "15"
}
],
"_id": "18"
}
]
In Compass View after add data upper data
Find doctor by name query
{ name: "Mavis Bxxxed" }
Find doctor by patient name
{ "patients.name": "Some one" }
If same search with this query
{ "patients.name": "Itch Txxx" }
OR
{ "patients.name": "Damon Wxxx" }
will same result to find the first docker (Mavis Bxxxed)
Due to it's patients start [ and end ]. it is array type a single size of data.
You can filter by project option
{
patients: {
$filter:
{
input: "$patients",
cond: { $eq: [ "$$patient.name", "Itch Txxx"] },
as: "patient"
}
}
}
OR shows only address
{ "patients.address" : 1 }
This Works
app.get('/api/v1/admin/getAllUsers',async function (req,res,next){
try {
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ') || !req.headers.authorization.split(' ')[1]) {
return res.status(422).json({ message: 'Please Provide Token!' })
}
dbConn.query('select * from fasta_users; select * from fasta_admin_users;select Name, email,password,phone,role,status,created_at from fasta_riders;', function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results , message: 'users list' });
});
} catch (err) {
next(err);
}
})
It returns the data i want without problems but the Json is Looking like this
{
"error": false,
"data": [
[
{
"id": 1,
"fastaUserId": "",
"fullname": "John Qasim",
"email": "j_qasim001#aol.com",
"password": "$2a$12$iAzLsIEcOJpDSSgdUmJBR.e8pWdieUg7N4bFiW0lkgBsQQDA6DwPG",
"state": "Imo",
"role": "Customers",
"city": "Owerri",
"phone_num": "08076631261",
"created_at": "2022-05-17T14:58:26.000Z"
},
{
"id": 2,
"fastaUserId": "FU-71198",
"fullname": "z",
"email": "z#gmail.com",
"password": "$2a$12$a9ibquFv477e6KL3N49JweVoiSmDXR0UVcM5xCkB39/8loKZ/u0bq",
"state": "z",
"role": "z",
"city": "user",
"phone_num": "z",
"created_at": "2022-05-17T17:57:39.000Z"
}
],
[
{
"id": 1,
"fastaUserId": "",
"fullname": "John Legend",
"email": "john.legend#gmail.com",
"password": "$2a$12$KrW0YKsJmvlp2z5uF4V10uLIjcPpSl1ba/LiWuiRpaLs/XRmguXDy",
"phone_num": "0188272610",
"avatar": "test_pic",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-11T17:48:39.000Z"
},
{
"id": 2,
"fastaUserId": "FA-76249",
"fullname": "James Hall",
"email": "jameshallblog.com#gmail.com",
"password": "$2a$12$j711FBY441CHwacisjcL9OT9Rjb5Yll8VzdSncKJilC0Pku9i2RP2",
"phone_num": "090865735422",
"avatar": "https://oxfordssocustomerapi.azurewebsites.net/Docs/Users/OIG-00424.jpg",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-17T17:51:03.000Z"
}
],
[
{
"Name": "Karen Mitchelle",
"email": "k.m.boon#gmail.com",
"password": "$2a$12$PCTK8okWO/ApoNSgXXTIl.oxzwqf.zlHHJaEvxtfxpUrnI1IaGNQC",
"phone": "08128829102",
"role": "Rider",
"status": "Active",
"created_at": "2022-05-17T14:59:22.000Z"
}
]
],
"message": "users list"
}
But I do not want this ^^^ above as a Response. I want something like this instead.
"data":[{
"id": 1,
"fastaUserId": "",
"fullname": "John Qasim",
"email": "j_qasim001#aol.com",
"password": "$2a$12$iAzLsIEcOJpDSSgdUmJBR.e8pWdieUg7N4bFiW0lkgBsQQDA6DwPG",
"state": "Imo",
"role": "Customers",
"city": "Owerri",
"phone_num": "08076631261",
"created_at": "2022-05-17T15:58:26.000Z"
},
{
"id": 1,
"fastaUserId": "",
"fullname": "alex roman",
"email": "alex.roman#juno.com",
"password": "$2a$12$sXpfYaQQIwoBf1KMaPrgT.PioHY4Y6SXD1lpheDB9EPTwfRtsg1p.",
"state": "Lagos",
"city": "city",
"phone_num": "08111111111",
"company_type": "Registered",
"company_name": "Polaris Shipping Inc.",
"company_regnum": "RC-123456",
"c_license": 1,
"role": "Delivery man",
"l_licensenumber": "LAG-2372328",
"company_address": "150 Wole Madariola Close",
"num_employees": "200",
"company_nature": "Sole Propietorship",
"created_at": "2022-05-17T15:57:41.000Z"
},
{
"id": 1,
"fastaUserId": "",
"fullname": "John Legend",
"email": "john.legend#gmail.com",
"password": "$2a$12$KrW0YKsJmvlp2z5uF4V10uLIjcPpSl1ba/LiWuiRpaLs/XRmguXDy",
"phone_num": "0188272610",
"avatar": "test_pic",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-11T18:48:39.000Z"
},
{
"id": 1,
"Name": "Karen Mitchelle",
"RidersID": "FR-60842",
"Status": "Active",
"email": "k.m.boon#gmail.com",
"password": "$2a$12$PCTK8okWO/ApoNSgXXTIl.oxzwqf.zlHHJaEvxtfxpUrnI1IaGNQC",
"phone": "08128829102",
"No_deliveries": "200",
"rating": "5",
"state": "Lagos",
"city": "Lagos",
"role": "Rider",
"address": "150 Wole Kolawole Str. Lagos Nigeria",
"bike_manufacturer": "Suzuki",
"bike_model": "Suzuki 400",
"bike_year": "2012",
"engine_power": "HP8",
"bike_color": "red",
"license_number": "SMK900-LA",
"license_expiry": "2027-05-01",
"vehicle_id": "LA8373Q2",
"created_at": "2022-05-17T15:59:22.000Z"
}
]
What must i do in my code above to access that data object from results in the response here like this return res.send({ error: false, data: results , message: 'users list' }); and return something like the second Json as shown above? I am trying to get this solved and nothing i have seen on the internet is being quite helpful here. Please i need help
I'm using the Shopify customer API to create customers. It all works well until I add a metafield of type JSON. Then, I get the response {"errors":{"value":"expected Hash to be a String"}}.
Here is the entire object I'm submitting:
{
"customer": {
"accepts_marketing": "true",
"accepts_marketing_updated_at": "2022-03-01T12:45:42.770",
"currency": "USD",
"default_address": {
"zip": "91321",
"city": "SomeCity",
"phone": "5555551111",
"customerCompany": "",
"countryCode": "US",
"default": "true",
"address1": "1111 My Street",
"address2": "",
"province_code": "CA",
"last_name": "LastusNamus",
"first_name": "FirstusNamus"
},
"email": "myemail#yahoo.com",
"first_name": "FirstusNamus",
"last_name": "LastusNamus",
"created_at": "2011-12-07T00:00:00",
"marketing_opt_in_level": "single_opt_in",
"note": "",
"order_count": 0,
"phone": "5555551111",
"state": "enabled",
"tax_exempt": "false",
"verfified_email": "true",
"metafields": [{
"namespace": "customer",
"key": "kickeeinfo",
"type": "json",
"value": {
"idcustomer": "37",
"iRewardPointsAccrued": "3000",
"iRewardPointsUsed": "0",
"idCustomerCategory": "0"
}
}]
}
}
Here is the string actually being submitted:
{"customer":{"accepts_marketing":"true","accepts_marketing_updated_at":"2022-03-01T12:45:42.770","currency":"USD","default_address":{"zip":"91321","city":"SomeCity","phone":"5555551111","customerCompany":"","countryCode":"US","default":"true","address1":"1111 My Street","address2":"","province_code":"CA","last_name":"LastusNamus","first_name":"FirstusNamus"},"email":"myemail#yahoo.com","first_name":"FirstusNamus","last_name":"LastusNamus","created_at":"2011-12-07T00:00:00","marketing_opt_in_level":"single_opt_in","note":"","order_count":0,"phone":"5555551111","state":"enabled","tax_exempt":"false","verfified_email":"true","metafields":[{"namespace":"customer","key":"kickeeinfo","type":"json","value":{"idcustomer":"37","iRewardPointsAccrued":"3000","iRewardPointsUsed":"0","idCustomerCategory":"0"}}]}}
What is the API expecting to be submitted? I've tried escaping the metafield "value" object and that doesn't work. I get an "unexpected token" error at that point.
The metafield value has to be escaped and then enclosed in quotes:
{
"customer": {
"accepts_marketing": "true",
"accepts_marketing_updated_at": "2022-03-01T12:45:42.770",
"currency": "USD",
"default_address": {
"zip": "91321",
"city": "SomeCity",
"phone": "5555551111",
"customerCompany": "",
"countryCode": "US",
"default": "true",
"address1": "1111 My Street",
"address2": "",
"province_code": "CA",
"last_name": "LastusNamus",
"first_name": "FirstusNamus"
},
"email": "myemail#yahoo.com",
"first_name": "FirstusNamus",
"last_name": "LastusNamus",
"created_at": "2011-12-07T00:00:00",
"marketing_opt_in_level": "single_opt_in",
"note": "",
"order_count": 0,
"phone": "5555551111",
"state": "enabled",
"tax_exempt": "false",
"verfified_email": "true",
"metafields": [{
"namespace": "customer",
"key": "kickeeinfo",
"type": "json",
"value": "{\"idcustomer\": \"37\",\"iRewardPointsAccrued\": \"3000\",\"iRewardPointsUsed\": \"0\",\"idCustomerCategory\": \"0\"}"
}]
}
}
I would like to transform my nested json message and take only required filed using Jolt spec-
My Input JSON :
{
"results": [
{
"user": {
"gender": "male",
"name": {
"title": "mr",
"first": "moritz",
"last": "zimmer"
},
"location": {
"street": "3667 breslauer straße",
"city": "gera",
"state": "mecklenburg-vorpommern",
"zip": 35662
},
"email": "moritz.zimmer#example.com",
"username": "purplegorilla905",
"password": "kkkkk",
"salt": "O5KFiJMB",
"md5": "d5659f39f9102452dca5e9afbfe06f07",
"sha1": "bb8790f5e22a8a4c33f12d623cd7f5c45adba275",
"sha256": "5fc5dd30d75c323404474e2c5bec2372a145f363742162b36a6414aa10e2b5e6",
"registered": 968202500,
"dob": 68248460,
"phone": "0450-7475136",
"cell": "0179-3008357",
"picture": {
"large": "https://randomuser.me/api/portraits/men/15.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/15.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/15.jpg"
}
}
},
{
"user": {
"gender": "female",
"name": {
"title": "ms",
"first": "julia",
"last": "bayer"
},
"location": {
"street": "5913 burgstraße",
"city": "mühldorf a. inn",
"state": "bremen",
"zip": 17247
},
"email": "julia.bayer#example.com",
"username": "bluedog987",
"password": "sheba1",
"salt": "qJ3KuIu7",
"md5": "c29e59c439ebc41b6772344b53863a17",
"sha1": "7fdb58f22eb85cb89ffb0c2cbb13bbf3bbb37238",
"sha256": "d6114912b8476e1c825a3487feb1cc7319c0c26f29f583ccd6864fa4f11214db",
"registered": 1224249359,
"dob": 1307176045,
"phone": "0846-0635929",
"cell": "0172-0881456",
"picture": {
"large": "https://randomuser.me/api/portraits/women/15.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/15.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/15.jpg"
}
}
},
{
"user": {
"gender": "male",
"name": {
"title": "mr",
"first": "sascha",
"last": "vogel"
},
"location": {
"street": "8276 lerchenweg",
"city": "straubing",
"state": "niedersachsen",
"zip": 73954
},
"email": "sascha.vogel#example.com",
"username": "purplefrog135",
"password": "picard",
"salt": "kBv4gmHR",
"md5": "79a388de1832ab7f14f4e5ddf0d88012",
"sha1": "8fc12f95217bf04eaf7e950154485dd35b84c65e",
"sha256": "a5df2bfd3ee84a869c43f92fe28ce5f56f7700bc75ce2e12bbee7cad2524af82",
"registered": 950549849,
"dob": 310749405,
"phone": "0098-5667969",
"cell": "0179-8473990",
"picture": {
"large": "https://randomuser.me/api/portraits/men/83.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/83.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/83.jpg"
}
}
},
{
"user": {
"gender": "female",
"name": {
"title": "mrs",
"first": "luisa",
"last": "döring"
},
"location": {
"street": "9027 am bahnhof",
"city": "salzgitter",
"state": "bayern",
"zip": 12419
},
"email": "luisa.döring#example.com",
"username": "purpleelephant855",
"password": "californ",
"salt": "gsnCNl8c",
"md5": "905d16801137f3368277889a783c02f3",
"sha1": "a5027752e9228bbc1e99fa56a6f2d09a1901e7e2",
"sha256": "51b7e09a415fef7e71eb3aec2721e91d0b51c2e264a7cdd41f3a692777771de3",
"registered": 1068886905,
"dob": 733574914,
"phone": "0648-0740938",
"cell": "0172-1199322",
"picture": {
"large": "https://randomuser.me/api/portraits/women/65.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/65.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/65.jpg"
}
}
}
],
"nationality": "DE",
"seed": "23e269027e0ea51404",
"version": "0.8"
}
Expected output :
[
{
"Gender": "male",
"FirstName": "moritz",
"LastName": "zimmer",
"City": "gera",
"State": "mecklenburg-vorpommern",
"Email": "moritz.zimmer#example.com",
"DateOfBirth": 68248460,
"MobileNo": "0179-3008357",
"Nationality": "DE"
},
{
"Gender": "female",
"FirstName": "julia",
"LastName": "bayer",
"City": "mühldorf a. inn",
"State": "bremen",
"Email": "julia.bayer#example.com",
"DateOfBirth": 1307176045,
"MobileNo": "0172-0881456",
"Nationality": "DE"
},
{
"Gender": "male",
"FirstName": "sascha",
"LastName": "vogel",
"City": "straubing",
"State": "niedersachsen",
"Email": "sascha.vogel#example.com",
"DateOfBirth": 310749405,
"MobileNo": "0179-8473990",
"Nationality": "DE"
},
{
"Gender": "female",
"FirstName": "luisa",
"LastName": "döring",
"City": "salzgitter",
"State": "bayern",
"Email": "luisa.döring#example.com",
"DateOfBirth": 733574914,
"MobileNo": "0172-1199322",
"Nationality": "DE"
}
]
I am using below Spec and output not coming as per expectation -
My Spec :
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"user": {
"gender": "[&2].Gender",
"name": {
"first": "[&3].FirstName",
"last": "[&3].LastName"
},
"location": {
"city": "[&3].City",
"state": "[&3].State"
},
"email": "[&2].Email",
"dob": "[&2].DateOfBirth",
"cell": "[&2].MobileNo"
}
}
}
}
}
]
Below my Spec output which is not as expected -
[
{
"Gender": "male",
"FirstName": "moritz",
"LastName": "zimmer",
"City": "gera",
"State": "mecklenburg-vorpommern",
"Email": "moritz.zimmer#example.com",
"DateOfBirth": 68248460,
"MobileNo": "0179-3008357"
},
{
"Gender": "female",
"FirstName": "julia",
"LastName": "bayer",
"City": "mühldorf a. inn",
"State": "bremen",
"Email": "julia.bayer#example.com",
"DateOfBirth": 1307176045,
"MobileNo": "0172-0881456"
},
{
"Gender": "male",
"FirstName": "sascha",
"LastName": "vogel",
"City": "straubing",
"State": "niedersachsen",
"Email": "sascha.vogel#example.com",
"DateOfBirth": 310749405,
"MobileNo": "0179-8473990"
},
{
"Gender": "female",
"FirstName": "luisa",
"LastName": "döring",
"City": "salzgitter",
"State": "bayern",
"Email": "luisa.döring#example.com",
"DateOfBirth": 733574914,
"MobileNo": "0172-1199322"
}
]
I tried many option but nationality is not coming inside as my expected output.
Please help here in Jolt transformation
Tip : Looking at outer part of the results array by using "*": "&" while looking at the inner part of that, and combining them under name key which seems to be unique as a grouping by criteria for the desired result set such as
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"user": {
"*": "&"
}
}
},
"*": "&"
}
},
{
"operation": "shift",
"spec": {
"name": {
"*": {
"#(2,gender[&])": "[&].Gender",
"#(0,first)": "[&1].First&2",
"#(0,last)": "[&1].Last&2",
"#(2,location[&].city)": "[&].City",
"#(2,location[&].state)": "[&].State",
"#(2,email[&])": "[&].Email",
"#(2,dob[&])": "[&].DateOfBirth",
"#(2,phone[&])": "[&].MobileNo",
"#(2,nationality)": "[&].Nationality"
}
}
}
}
]
I have the following sample JSON
"address":{
"details":
[
{
"id" :<system generated UUID>,
"city" :{"type" : "string"},
"country" :{"type" : "string"},
"name" :{"type" : "string"},
"url" :{"type" : "string"},
"contacts" :
[{
"email":{"type" : "string"},
"phone":{"type" : "number"}
}]
}
]
}
I want the data to be in the following ways
Example 1:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk"
}]
}
Example 2:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
}
]
}]
}
Example 3 :
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
},
{
"email": "sri123#gmail.com",
"phone": 9876655555
}
]
}]
}
Example 4:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
}
]
},
{
"id": 3456,
"city": "Texas",
"country": "USA",
"name": "Texas Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "ram123#gmail.com",
"phone": 876543219
}
]
}
]
}
I am very new to n1ql query, would anyone please help me to find the solution for inserting/Update an array to the object with in object. Example 1 is inserting an data to JSON, Example 2 is updating the data to Example 1 and follows...
1. Please help to get n1ql queries that satisfies above examples.
2. How to generate an UUID to id field in the document
1) Appends array never checks if element already exist in the array
INSERT INTO default VALUES ("k01", { "details": [ { "id": 1234, "city": "Medway", "country": "United Kingdom", "name": "Medway Youth Hostel", "url": "http://www.yha.org.uk" }] });
UPDATE default AS d SET e.contact = [ { "email": "hari34#gmail.com", "phone": 6789435634 } ]
FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";
UPDATE default AS d SET e.contact = ARRAY_APPEND(e.contact,{ "email": "sri123#gmail.com", "phone": 9876655555 })
FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";
UPDATE default AS d SET d.details = ARRAY_APPEND(d.details,{ "id": 3456, "city": "Texas", "country": "USA", "name": "Texas Youth Hostel", "url": "http://www.yha.org.uk", "contact": [ { "email": "ram123#gmail.com", "phone": 876543219 } ] })
WHERE META().id = "k01";
SELECT * from default USE KEYS["k01"];
2) USE UUID() function like below
INSERT INTO default VALUES ("k01", { "details": [ { "id": UUID(), "city": "Medway", "country": "United Kingdom", "name": "Medway Youth Hostel", "url": "http://www.yha.org.uk" }] });