How to verify that i get only one key from JSON response? - json

I get a response in JSON and need to make sure that i get only one 'signal' in signal_events. I'm using Ruby with rspec.
"signal_events": [
{
"id": "587e9ae969702d10bd5a0000",
"created_at": 1484692201,
"geo": {
"type": "Point",
"coordinates": [
-153.45703125,
59.67773438
]
},
"expires_at": 1484778601,
"geohashes": [
"bddg",
"bdeh"
],
"signal": {
"id": "587e9ae969702d0911060000",
"created_at": 1484692201.24,
"expires_at": 1484778601.24,
"signal_at": 1484691607,
"source": "usgs",
"updated_at": 1484692144,
"magnitude": 2,
"radius": 6.36107901750268,
"event_name": "earthquake",
"tsunami": "no"
},
"signal_type": "earthquake",
"centroid": {
"type": "Point",
"coordinates": [
-153.45703125,
59.67773438
]
},
"location": {
"country": "United States",
"country_code": "US",
"city": "Kenai Peninsula Borough",
"region": "Kenai Peninsula Borough",
"region_code": "AK"
}
},

Hash can not contain duplicated keys, so you will always have one value for any given key.

Related

JSON Parsing a Plain Text File

I have a lot of data I need to parse though.
I need to pull all pid's and price's.
`
[
{
"id": 159817,
"price": "10.69",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 24549,
"pid": "12079",
"labels": [
{
"type": "default",
"value": "Chicken Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Chicken",
"locale": "en"
},
{
"type": "product_json",
"value": "Chicken",
"locale": "en"
}
],
"calMin": 600,
"calMax": 600,
"lastUpdated": "2021-12-31T13:49:22.794Z"
}
},
{
"id": 159818,
"price": "9.29",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 25,
"pid": "1",
"labels": [
{
"type": "default",
"value": "Ham Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Ham",
"locale": "en"
}
],
"calMin": 540,
"calMax": 540,
"lastUpdated": "2021-07-09T19:30:00.326Z"
}
}
]
`
and I need to place them into a string like this, but on a scale of 150 products. I'd also need to change "pid" to "productId"
[{ "productId": "46238", "price": 6.09 }, { "productId": "40240", "price": 1.49 }]
I need to add a string before this data, but I'm pretty confident I can figure that part out.
I am pretty open to the easiest suggestion, whether that be VBS, Excel macro, etc.

Mapping data from nested JSON files

I'm trying to use a map function to pass all of the country names for a given continent into React components. I think I might need to convert this to an array, but I don't know how I would access the name key for each object given the objects all have different names.
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
},
"AG": {
"name": "Antigua and Barbuda",
"native": "Antigua and Barbuda",
"phone": "1268",
"continent": "NA",
"capital": "Saint John's",
"currency": "XCD",
"languages": [
"en"
]
},
"AI": {
"name": "Anguilla",
"native": "Anguilla",
"phone": "1264",
"continent": "NA",
"capital": "The Valley",
"currency": "XCD",
"languages": [
"en"
]
}
You can try the following
var countryNames = [];
for(let key in YourJsonData){
countryNames.push(YourJsonData[key][name]);
}
then loop over that array and render the elements in your component

How to extract a specific value from JSON file?

I'm trying to extract a specific value from JSON file.
the key value is: "info": "this is an example" (The key is unique)
I want to extract only the value: "this is an example"
My code:
cat 9.json | jq '.info'
result:
null
JSON file example:
{
"Event": {
"id": "13",
"orgc_id": "1",
"org_id": "1",
"date": "2019-01-09",
"threat_level_id": "3",
"info": "test9",
"published": false,
"uuid": "5c35d180",
"attribute_count": "2",
"analysis": "0",
"timestamp": "1547044733",
"distribution": "1",
"proposal_email_lock": false,
"locked": false,
"publish_timestamp": "1547034089",
"sharing_group_id": "0",
"disable_correlation": false,
"extends_uuid": "",
"event_creator_email": "o#cyhgfnt.com",
"Org": {
"id": "1",
"name": "Cygfdgfdnt",
"uuid": "5b9f938d-e3a0-4ecb-83b3-0bdeac1b41bc"
},
"Orgc": {
"id": "1",
"name": "Cyhgfgft",
"uuid": "5b9f938d-e3a0-4ecb-83b3-0bdeac1b41bc"
},
"Attribute": [{
"id": "292630",
"type": "domain",
"category": "Network activity",
"to_ids": true,
"uuid": "5c35dd94-cccc-4086-b386-682823717aa5",
"event_id": "1357",
"distribution": "5",
"timestamp": "1547034584",
"comment": "This is a comment",
"sharing_group_id": "0",
"deleted": false,
"disable_correlation": false,
"object_id": "0",
"object_relation": null,
"value": "dodskj.com",
"Galaxy": [],
"ShadowAttribute": [],
"Tag": [{
"id": "223",
"name": "kill-chain:Exploitation",
"colour": "#a80079",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}, {
"id": "292631",
"type": "ip-dst",
"category": "Network activity",
"to_ids": true,
"uuid": "5c35dd94-fe90-4ef6-b3a9-682823717aa5",
"event_id": "1357",
"distribution": "5",
"timestamp": "1547044733",
"comment": "comment example",
"sharing_group_id": "0",
"deleted": false,
"disable_correlation": false,
"object_id": "0",
"object_relation": null,
"value": "8.8.6.6",
"Galaxy": [],
"ShadowAttribute": [],
"Tag": [{
"id": "247",
"name": "maec-malware-capabilities:maec-malware-capability=\"anti-removal\"",
"colour": "#3f0004",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}, {
"id": "465",
"name": "osint:lifetime=\"perpetual\"",
"colour": "#006ebe",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}],
"ShadowAttribute": [],
"RelatedEvent": [],
"Galaxy": [{
"id": "3",
"uuid": "698774c7-8022-42c4-917f-8d6e4f06ada3",
"name": "Threat Actor",
"type": "threat-actor",
"description": "Threat actors are characteristics of malicious actors (or adversaries) representing a cyber attack threat including presumed intent and historically observed behaviour.",
"version": "3",
"icon": "user-secret",
"namespace": "misp",
"GalaxyCluster": [{
"id": "6397",
"collection_uuid": "7cdff317-a673-4474-84ec-4f1754947823",
"type": "threat-actor",
"value": "Sofacy",
"tag_name": "misp-galaxy:threat-actor=\"Sofacy\"",
"description": "The Sofacy Group (also known as APT28, Pawn Storm, Fancy Bear and Sednit) is a cyber espionage group believed to have ties to the Russian government. Likely operating since 2007, the group is known to target government, military, and security organizations. It has been characterized as an advanced persistent threat.",
"galaxy_id": "3",
"source": "MISP Project",
"authors": ["Alexandre Dulaunoy", "Florian Roth", "Thomas Schreck", "Timo Steffens", "Various"],
"version": "82",
"uuid": "5b4ee3ea-eee3-4c8e-8323-85ae32658754",
"tag_id": "608",
"meta": {
"cfr-suspected-state-sponsor": ["Russian Federation"],
"cfr-suspected-victims": ["Georgia", "France", "Jordan", "United States", "Hungary", "World Anti-Doping Agency", "Armenia", "Tajikistan", "Japan", "NATO", "Ukraine", "Belgium", "Pakistan", "Asia Pacific Economic Cooperation", "International Association of Athletics Federations", "Turkey", "Mongolia", "OSCE", "United Kingdom", "Germany", "Poland", "European Commission", "Afghanistan", "Kazakhstan", "China"],
"cfr-target-category": ["Government", "Military"],
"cfr-type-of-incident": ["Espionage"],
"country": ["RU"],
"refs": ["https:\/\/en.wikipedia.org\/wiki\/Sofacy_Group", "https:\/\/aptnotes.malwareconfig.com\/web\/viewer.html?file=..\/APTnotes\/2014\/apt28.pdf", "http:\/\/www.trendmicro.com\/cloud-content\/us\/pdfs\/security-intelligence\/white-papers\/wp-operation-pawn-storm.pdf", "https:\/\/www2.fireeye.com\/rs\/848-DID-242\/images\/wp-mandiant-matryoshka-mining.pdf", "https:\/\/www.crowdstrike.com\/blog\/bears-midst-intrusion-democratic-national-committee\/", "http:\/\/researchcenter.paloaltonetworks.com\/2016\/06\/unit42-new-sofacy-attacks-against-us-government-agency\/", "https:\/\/www.cfr.org\/interactive\/cyber-operations\/apt-28", "https:\/\/blogs.microsoft.com\/on-the-issues\/2018\/08\/20\/we-are-taking-new-steps-against-broadening-threats-to-democracy\/", "https:\/\/www.bleepingcomputer.com\/news\/security\/microsoft-disrupts-apt28-hacking-campaign-aimed-at-us-midterm-elections\/", "https:\/\/www.bleepingcomputer.com\/news\/security\/apt28-uses-lojax-first-uefi-rootkit-seen-in-the-wild\/"],
"synonyms": ["APT 28", "APT28", "Pawn Storm", "PawnStorm", "Fancy Bear", "Sednit", "TsarTeam", "Tsar Team", "TG-4127", "Group-4127", "STRONTIUM", "TAG_0700", "Swallowtail", "IRON TWILIGHT", "Group 74"]
}
}]
}],
"Object": [],
"Tag": [{
"id": "608",
"name": "misp-galaxy:threat-actor=\"Sofacy\"",
"colour": "#12e000",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}, {
"id": "118",
"name": "gdpr:special-categories=\"health\"",
"colour": "#3ce600",
"exportable": true,
"user_id": "0",
"hide_tag": false,
"numerical_value": null
}]
}
}
I suppose you are trying to get the .info field inside .Event which should have been written as below. Use -r for without quotes
jq '.Event.info'

Couchbase N1ql query for updating new objet to JSON array

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" }] });

Fetching per segment price from Google QPX Express API

Using this url: https://qpx-express-demo.itasoftware.com/ I am making a request to get flight availability from BOM to NYC on a future date.
The JSON response that i receive :
Formatted Response:
Solution# 1 Sale Price: INR31803
Slice 0
KU 302 BOM 2016-01-29T05:50+05:30 KWI 2016-01-29T07:50+03:00
KU 117 KWI 2016-01-29T09:55+03:00 JFK 2016-01-29T16:20-05:00
Unformatted Response:
{
"kind": "qpxexpress#tripOption",
"saleTotal": "INR31803",
"id": "5WAPnfYj8mARdie1jBKphP002",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 1260,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 270,
"flight": {
"carrier": "KU",
"number": "302"
},
"id": "G9ieitLhcbGEpU6u",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 9,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "LRlNO3Gw0UX8y69J",
"aircraft": "777",
"arrivalTime": "2016-01-29T07:50+03:00",
"departureTime": "2016-01-29T05:50+05:30",
"origin": "BOM",
"destination": "KWI",
"originTerminal": "2",
"destinationTerminal": "M",
"duration": 270,
"mileage": 1713,
"meal": "Breakfast",
"secure": true
}
],
"connectionDuration": 125
},
{
"kind": "qpxexpress#segmentInfo",
"duration": 865,
"flight": {
"carrier": "KU",
"number": "117"
},
"id": "GBan4WxY2pcKVHbu",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 9,
"marriedSegmentGroup": "1",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "L9pmI3XsVZSBhZah",
"aircraft": "777",
"arrivalTime": "2016-01-29T16:20-05:00",
"departureTime": "2016-01-29T09:55+03:00",
"origin": "KWI",
"destination": "JFK",
"originTerminal": "M",
"destinationTerminal": "4",
"duration": 865,
"mileage": 6336,
"meal": "Meal",
"secure": true
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"carrier": "KU",
"origin": "BOM",
"destination": "NYC",
"basisCode": "HLOWIN1"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"segmentId": "G9ieitLhcbGEpU6u"
},
{
"kind": "qpxexpress#segmentPricing",
"fareId": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"segmentId": "GBan4WxY2pcKVHbu"
}
],
"baseFareTotal": "INR26460",
"saleFareTotal": "INR26460",
"saleTaxTotal": "INR5343",
"saleTotal": "INR31803",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "JN_004",
"chargeType": "GOVERNMENT",
"code": "JN",
"country": "IN",
"salePrice": "INR1482"
},
{
"kind": "qpxexpress#taxInfo",
"id": "F2_001",
"chargeType": "GOVERNMENT",
"code": "F2",
"country": "IN",
"salePrice": "INR53"
},
{
"kind": "qpxexpress#taxInfo",
"id": "IN",
"chargeType": "GOVERNMENT",
"code": "IN",
"country": "IN",
"salePrice": "INR627"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YM_001",
"chargeType": "GOVERNMENT",
"code": "YM",
"country": "IN",
"salePrice": "INR687"
},
{
"kind": "qpxexpress#taxInfo",
"id": "WO_001",
"chargeType": "GOVERNMENT",
"code": "WO",
"country": "IN",
"salePrice": "INR149"
},
{
"kind": "qpxexpress#taxInfo",
"id": "US_002",
"chargeType": "GOVERNMENT",
"code": "US",
"country": "US",
"salePrice": "INR1190"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YX_001",
"chargeType": "GOVERNMENT",
"code": "YX",
"country": "KW",
"salePrice": "INR54"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YC",
"chargeType": "GOVERNMENT",
"code": "YC",
"country": "US",
"salePrice": "INR368"
},
{
"kind": "qpxexpress#taxInfo",
"id": "XY",
"chargeType": "GOVERNMENT",
"code": "XY",
"country": "US",
"salePrice": "INR468"
},
{
"kind": "qpxexpress#taxInfo",
"id": "XA",
"chargeType": "GOVERNMENT",
"code": "XA",
"country": "US",
"salePrice": "INR265"
}
],
"fareCalculation": "BOM KU X/KWI KU NYC 397.64HLOWIN1 NUC 397.64 END ROE 66.541160 FARE INR 26460 XT 53F2 627IN 1482JN 149WO 687YM 54YX 368YC 468XY 265XA 1190US",
"latestTicketingTime": "2016-01-28T19:19-05:00",
"ptc": "ADT",
"refundable": true
}
From the response it is inferred that there are two segments, one with KU 302 and other with KU 117. How can i get per segment (per flight) info from the response?
I'm using the APIs in C#, if you install the Json.NET NuGet package, you can then get the response and deserialize it with:
JsonConvert.DeserializeObject<Response>(jsonString);
To build the Response object you can use an online Json to C# class builder (Google it and you'll find it immediately), or you can use dynamic objects.
Once you have the deserialized responde, you can just use LINQ to filter the informations:
from r in response where <filters> select r.segment // Or specific data.
All this assuming you want to use C#.