get the matching part out of cloudant json - json

I'm working with cloudantDB as DB for my spring application.
The thing that I'm trying to do is:
check if there is a timeslot and topic in the document
if there is a topic filled in:
check the userId who has submitted that topic and return all of the items for that user.
{
"_id": "393c7748cf487f7c5223839dfdf5adf9",
"_rev": "2-889c6d9d14fcc3c90a00e91aca5f2f4c",
"name": "conference 1",
"type": "conference",
"location": "conf loc",
"tag": "confTag",
"startDate": "2015-05-04T22:00:00.000+0000",
"endDate": "2015-05-08T22:00:00.000+0000",
"timeslots": {
"8a03b160-48e3-4f99-be38-2a2da34e5890": {
"id": "8a03b160-48e3-4f99-be38-2a2da34e5890",
"startDate": "2015-04-07T10:45:00.000+0000",
"endDate": "2015-05-07T12:45:00.000+0000",
"location": "Location",
"topic": {
"id": "4ad235ef-5938-4461-8a2e-346bf105c5b6",
"title": "wa nen coole title",
"description": "dees is een goei omschrijving",
"tags": "cloud, tag",
"attachment": "file.exe",
"speakerId": null
}
},
"a59b1dfd-d1c5-491f-bc02-807e38595ba4": {
"id": "a59b1dfd-d1c5-491f-bc02-807e38595ba4",
"startDate": "2015-05-05T10:00:00.000+0000",
"endDate": "2015-05-05T11:15:00.000+0000",
"location": "mlkjs",
"topic": {
"id": "8b753adb-9a15-486f-9209-b9cc5ef134a1",
"title": "test132",
"description": "met speakerId",
"tags": "sldkjf",
"attachment": "kklsjdf",
"speakerId": "speaker"
}
},
"080153e7-f8bc-453e-b49a-a1df4679ceef": {
"id": "080153e7-f8bc-453e-b49a-a1df4679ceef",
"startDate": "2015-05-09T11:30:00.000+0000",
"endDate": "2015-05-09T13:30:00.000+0000",
"location": "file upload",
"topic": {
"id": "e3533a60-a0ba-4439-a9e9-b29e69be47db",
"title": "fdgdfg",
"description": "fdsgdsggdfgdfg",
"tags": "dfgsdg",
"attachment": null,
"speakerId": null
}
}
},
"defaultTwitterText": "Hallo",
"twitterUrl": "https://twitter.com/intent/tweet?button_hashtag=confTag&text=Hallo"
}
So far I have this:
function(doc) {
{ if (doc.type === 'conference') {
if(doc.timeslots.size !==0 ){
emit( null, doc);
}
}
}
}
Thanks!

I think you might want something like:
function (doc) {
if (doc.type == 'conference'){
if (doc.timeslots){
for (var i in doc.timeslots) {
if(doc.timeslots[i].topic){
emit(doc.timeslots[i].topic.speakerId, doc.timeslots[i])
}
};
}
}
}
that'll make a view of key:speaker, value: topic object, which you can then query with ?key=speaker to see the list of all the topic objects for the speaker.

Related

Get depictions from Wikimedia Commons

Wikimedia Commons has a new feature to describe what a picture depicts. Example:
Question
How to get the "depicts" of a given image, as JSON, using either a wikimedia-android-data-client library call or a REST HTTP request?
You can use some of the Wikibase APIs (exactly which or how is not well-documented at the moment, I think). E.g.
https://commons.wikimedia.org/w/api.php?action=wbgetclaims&format=jsonfm&entity=M78893847
{
"claims": {
"P180": [
{
"mainsnak": {
"snaktype": "value",
"property": "P180",
"hash": "7754bb4b4dcf94dd0a6b8524351b22df21328a88",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 12280,
"id": "Q12280"
},
"type": "wikibase-entityid"
},
"datatype": "wikibase-item"
},
"type": "statement",
"id": "M78893847$bf7f116a-4d08-0426-0e81-c9552a89fa63",
"rank": "preferred"
},
{
"mainsnak": {
"snaktype": "value",
"property": "P180",
"hash": "8ed7e126588c21d3cf88387d5ee875d528c00a74",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 1021645,
"id": "Q1021645"
},
"type": "wikibase-entityid"
},
"datatype": "wikibase-item"
},
"type": "statement",
"id": "M78893847$7830fc04-4a1e-f289-93bc-69d6bbf3b7fd",
"rank": "normal"
}
]
}
}
78893847 being the page ID which you can get via the query API:
https://commons.wikimedia.org/w/api.php?action=query&format=jsonfm&formatversion=2&titles=File%3AMomoyo-brug%20in%20Shibaura%2C%20gezien%20richting%20het%20zuidoosten%2C%20-15%20maart%202019.jpg
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 78893847,
"ns": 6,
"title": "File:Momoyo-brug in Shibaura, gezien richting het zuidoosten, -15 maart 2019.jpg"
}
]
}
}

Parse Complex Json Data with VB.net and Newtonsoft.Json

I'm building a windows application using vb.net to take a Json file that will be downloaded daily and parse it and enter the values into a SQL database. The only part I'm getting stuck on is being about to parse the Json data to get the values I need.
Public Sub json_parse(ByVal result_json As String)
Try
Dim json As String = result_json
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "data"
For Each msg As JObject In item.Values
MsgBox(msg.ToString)
Next
End Select
Next
Catch __unusedException1__ As Exception
'MsgBox(__unusedException1__.ToString)
End Try
End Sub
{
"data": {
"wsj_mdstrip_na,us": {
"set": 1562756496071,
"ttl": 300000,
"data": {
"id": "na,us",
"type": "wsj_mdstrip",
"data": [
{
"timeZone": "CDT",
"localTimeZoneTimestamp": "5:51 AM CDT 07/10/19",
"localTimestamp": "5:51 AM 07/10/19",
"etTimestamp": "6:51 AM ET 07/10/19",
"mdTimestamp": "2019-07-10T06:51:35-04:00",
"timestamp": "2019-07-10T05:51:35.365",
"chartingSymbol": "Future/US/XCBT/YM00",
"updatedTimestamp": "2019-07-10T07:01:36-04:00"
},
{
"deltaBarPosNeg": "deltaBar-neg",
"etTimestamp": "6:51 AM ET 07/10/19",
"chartingSymbol": "Future/US/XCME/ES00",
"pastCloses": [
{
"range": "P1Y",
"price": 2775.75
},
{
"range": "P3Y",
"price": 2145.75
}
]
}
]
}
},
"wsj_nav_na,us": {
"set": 1562754843231,
"ttl": 1800000,
"isStale": false
},
"mdc_cashprices_": {
"set": 1562756402351,
"ttl": 90000,
"data": {
"id": "{\"requestedCommodities\":\"all\",\"groupIntoMapBy\":\"type\",\"groupIntoSetsBy\":\"subType\"}",
"type": "mdc_cashprices",
"data": {
"instruments": [
{
"code": null,
"djShortName": "Gold-EnglehardFab",
"instrumentID": "511498",
"name": "Engelhard fabricated products",
"subType": "Gold, per troy oz",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1500.72",
"previousDay": "1509.33",
"previousYear": "1351.58"
},
{
"code": null,
"djShortName": "Silver-EngelhrdFab",
"instrumentID": "511558",
"name": "Engelhard fabricated products",
"subType": "Silver, troy oz.",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "18.0600",
"previousDay": "18.0960",
"previousYear": "19.3200"
},
{
"code": null,
"djShortName": "SilverHandy&HarmB",
"instrumentID": "511560",
"name": "Handy & Harman base price",
"subType": "Silver, troy oz.",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "15.1070",
"previousDay": "15.0420",
"previousYear": "16.1000"
},
{
"code": null,
"djShortName": "Platinum-EngelhFab",
"instrumentID": "511554",
"name": "Platinum, Engelhard fabricated products",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "912.0",
"previousDay": "914.0",
"previousYear": "946.0"
},
{
"code": null,
"djShortName": "Palladium-EngelInd",
"instrumentID": "511553",
"name": "Palladium, Engelhard industrial bullion",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1561.0",
"previousDay": "1573.0",
"previousYear": "955.0"
},
{
"code": null,
"djShortName": "Palladium-EngelFab",
"instrumentID": "511552",
"name": "Palladium, Engelhard fabricated products",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1661.0",
"previousDay": "1673.0",
"previousYear": "1055.0"
}
],
"timestamp": "7/09/19"
}
}
},
"consumer_corphat_corphat": {
"set": 1562756495973,
"ttl": 90000,
"data": {
"id": "corphat",
"type": "consumer_corphat",
"data": [
{
"ncLinks": [
{
"title": "Big Decisions",
"url": "http://www.bigdecisions.com/",
"nofollow": "false"
},
{
"title": "Business Spectator",
"url": "https://www.businessspectator.com.au/",
"nofollow": "false"
}
]
},
{
"djLinks": [
{
"title": "Barron's",
"url": "https://www.barrons.com",
"nofollow": "false"
},
{
"title": "BigCharts",
"url": "http://bigcharts.marketwatch.com",
"nofollow": "false"
}
]
}
]
},
"isStale": false
}
},
"currentState": {
"data": [
"0.0.3.0.0",
"0.0.3.1.0.0.2"
],
"components": {
"code___decoratedComponent___275181c7-8620-4df3-a008-d0cd9937db22___WSJTheme---WSJBase---WSJForms---WSJTables": {
"id": "275181c7-8620-4df3-a008-d0cd9937db22",
"decorators": [
"WSJTheme",
"WSJBase",
"WSJForms",
"WSJTables"
]
},
"code___decoratedComponent___c8882c9c-15d3-4d1f-9b0e-81b6f321365d___WSJTheme---WSJBase---WSJForms---WSJTables": {
"id": "c8882c9c-15d3-4d1f-9b0e-81b6f321365d",
"decorators": [
"WSJTheme",
"WSJBase",
"WSJForms",
"WSJTables"
]
}
}
}
}
I'm trying to get the values for "last", "previousDay" and "previousYear" from:
data > mdc_cashprices_ > data > instruments > (where djShortName = "Gold-EnglehardFab" and djShortName = "Silver-EngelhrdFab" and djShortName = "Platinum-EngelhFab" and djShortName = "Palladium-EngelFab")
I'm also trying to get the value for "timestamp" from:
data > mdc_cashprices_ > data > timestamp
In my current code I can only get to the 1st "data" tier.
Any help would be greatly appreciated.
Use something like https://jsonformatter.curiousconcept.com/ to view it more easily.
Then simply
Dim Something as JObject = JObject.Parse(<your JSON>)
Then get what you want with
Something1 = Something("data")("mdc_cashprices")("data")("data")("instruments")(0)("last") 'basically you just traverse the levels, you use the id/text for regular stuff (inside {}) and numbers for arrays (inside [])
Since it's an array you would probably want to loop it
For Each item In Something("data")("mdc_cashprices")("data")("data")("instruments")
Something2 = item("last")
Next
Btw there are some formatting issues here >>> mdc_cashprices_{\"req... <<< that _ and \ shouldn't be there"

How to get a value of a object from its foreign key?

Im using Backand to store my data. I have an object, Events, that references another object, Locations.
{
"name": "events",
"fields": {
"eventCommentsId": {
"collection": "comments",
"via": "eventId"
},
"tags": {
"collection": "events_tags",
"via": "event"
},
"users": {
"collection": "users_events",
"via": "event"
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
{
"name": "locations",
"fields": {
"events": {
"collection": "events",
"via": "locationId"
},
"name": {
"type": "text"
},
"geo": {
"type": "point"
}
}
}
When I try to display the location of the event, I can only get the value of locationID. I want the actual name of the location, not the id. How do I do that?
<ion-list>
<ion-item class="item item-thumbnail-left" ng-repeat="event in events" type="item-text-wrap" href="#/event-detail/{{event.id}}">
<h2>{{event.name}}</h2>
<p><i class="ion-location"></i> {{event.locationId.name}}</p>
<ion-option-button class="button-assertive" ng-click="deleteEvent(event.id)">
Delete
</ion-option-button>
</ion-item>
</ion-list>
angular code
.service('EventService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'events/';
function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}
function getUrlForId(id) {
return getUrl() + id;
}
getEvents = function () {
return $http.get(getUrl());
};
addEvent = function(event) {
return $http.post(getUrl(), event);
}
deleteEvent = function (id) {
return $http.delete(getUrlForId(id));
};
getEvent = function (id) {
return $http.get(getUrlForId(id));
};
return {
getEvents: getEvents,
addEvent: addEvent,
deleteEvent: deleteEvent,
getEvent: getEvent
}
})
.controller('FeedCtrl', ['$scope', '$ionicModal', '$ionicSideMenuDelegate', 'EventService', function($scope, $ionicModal, $ionicSideMenuDelegate, EventService) {
$scope.events = [];
$scope.input = {};
function getAllEvents() {
EventService.getEvents()
.then(function (result) {
$scope.events = result.data.data;
});
}
$scope.addEvent = function() {
EventService.addEvent($scope.input)
.then(function(result) {
$scope.input = {};
getAllEvents();
});
}
$scope.deleteEvent = function(id) {
EventService.deleteEvent(id)
.then(function (result) {
getAllEvents();
});
}
getAllEvents();
}])
There are two options. You can either use the descriptive value in the __metadata of each object like this:
request: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1
response:
{
"totalRows": 2,
"data": [
{
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 1,
"name": "knicks vs warriors",
"date": null,
"time": null,
"info": "",
"locationId": "1"
},
{
"__metadata": {
"id": "2",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 2,
"name": "knicks vs cavs",
"date": null,
"time": null,
"info": "",
"locationId": "1"
}
]
}
or you can do a deep request and get the value in the relatedObjects
request: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1&deep=true
response:
{
"totalRows": 2,
"data": [
{
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 1,
"name": "knicks vs warriors",
"date": null,
"time": null,
"info": "",
"locationId": "1"
},
{
"__metadata": {
"id": "2",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 2,
"name": "knicks vs cavs",
"date": null,
"time": null,
"info": "",
"locationId": "1"
}
],
"relatedObjects": {
"locations": {
"1": {
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"events": {
"collection": "events",
"via": "locationId"
},
"name": {
"type": "text"
},
"geo": {
"type": "point"
}
},
"descriptives": {},
"dates": {}
},
"id": 1,
"events": null,
"name": "Madison Square Garden",
"geo": [
40.7505,
73.9934
]
}
}
}
}
search for Madison Square Garden as the name of the location to understand the JSON structure.
You can set the descriptive field in the Object Settings

json parsing: GET elements if their exist in each in insertion

I need to get all the data from my json FB file.
Some insertion for example, few fields have "likes" but others dont.
I need to check if the field "like" exists
An example of my data is this:
{
"userdata":
[
{
"id": "xxxxxxxxxxxxxxxxxxxx",
"from": {
"name": "my name",
"id": "xxxxxxxxxxxxxxxxxxxx"
},
"message": "Hello",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/"
},
{
"name": "Like",
"link": "https://www.facebook.com/"
}
],
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"application": {
"name": "Graph API Explorer",
"id": "xxxxxxxxxxxxxxxxxxxx"
},
"created_time": "2013-12-25T15:22:43+0000",
"updated_time": "2013-12-25T15:22:43+0000",
**"likes"**: {
"data": [
{
"id": "xxxxxxxxxxxxxxxxxxxx",
"name": "friend1"
},
{
"id": "xxxxxxxxxxxxxxxxxxxx",
"name": "friend2"
}
],
"paging": {
"cursors": {
"after": "xxxxxxxxxxxxxxxxxxxx==",
"before": "xxxxxxxxxxxxxxxxxxxx=="
}
}
}
},
{
"id": "xxxxxxxxxxxxxxxxxxxx",
"from": {
"name": "name3",
"id": "xxxxxxxxxxxxxxxxxxxx"
},
"to": {
"data": [
{
"name": "my name",
"id": "xxxxxxxxxxxxxxxxxxxx"
}
]
},
"message": "hello again! ",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/xxxxxxxxxxxxxxxxxxxx"
},
{
"name": "Like",
"link": "https://www.facebook.com/xxxxxxxxxxxxxxxxxxxx"
}
],
"privacy": {
"value": ""
},
"type": "status",
"application": {
"name": "Facebook for iPad",
"namespace": "fbipad_",
"id": "xxxxxxxxxxxxxxxxxxxx"
},
"created_time": "2013-12-17T20:34:04+0000",
"updated_time": "2013-12-17T20:34:04+0000"
}
]
}
I use the following code but I get only the first insertion and not both because the second does not have the field "likes".
<script>
$(document).ready(function(){
});
$("#loaduserdata").click(function(){
$("#userdata tbody").html("");
$.getJSON("js.json", function(data){
$.each(data.userdata, function(i,user){
var tblRow =
"<tr>"
+"<td>FEED FROM: "+user.from.name+"</td>"
+"<td>MESSAGE: "+user.message+"</td>"
+"<td>created_time: "+user.created_time+"</td>"
+"<td>updated_time: "+user.updated_time+"</td>"
+ "<td>LIKES TO" + (typeof user.likes.data == "undefined" ? "" : user.likes.data.map(function(item) { return " "+item.name; }).join(", ")) + "</td>"
+"</tr>"
$(tblRow).appendTo("#userdata tbody");
});
}
);
});
</script>
HELP please.
You forgot to check if user.likes exists and only check for user.likes.data, but that check already throws an error because you try to access the property data on an undefined object.
function getLikesFromUser(user) {
if (user.likes && user.likes.data) {
return user.likes.data.map(function(item) {
return item.name;
}).join(", ");
} else {
return "";
}
}
Fixed Example on Plunker

Dropdown list from google spreadsheet

This fiidle have a sample dropdown list which data is comes from hard coded json format as given below.
var data = [
{
"department": "IT",
"subject": [
{
"title": "Programmer",
"services": [
{
"name": "example1"},
{
"name": "example2"}
]
},
{
"title": "Solutions Architect",
"services": [
{
"name": "example3"},
{
"name": "example4"}
]
},
{
"title": "Database Developer"}
]},
{
"department": "Accounting",
"subject": [
{
"title": "Accountant"},
{
"title": "Payroll Officer"},
{
"title": "Accounts Clerk"},
{
"title": "Analyst"},
{
"title": "Financial Controller"}
]},
{
"department": "HR",
"subject": [
{
"title": "Recruitment Consultant"},
{
"title": "Change Management"},
{
"title": "Industrial Relations"}
]},
{
"department": "Marketing",
"subject": [
{
"title": "Market Researcher"},
{
"title": "Marketing Manager"},
{
"title": "Marketing Co-ordinator"}
]}
]
Is it possible to create the same type of dropdown with data in google spreadsheet? If so, what is the data format required in spreadsheet?
EDIT: Is it possible to create the same dropdown menu using data source something as
var query = new google.visualization.Query("http://spreadsheets.google.com/tq?key=0AozvCNI02VmpdDBKYUhRdVVhb2xGel91c2FkV1daYkE&single=true&gid=0&pub=1");
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
var data = response.getDataTable();
which is coming from this spreadsheet