Why is it that when I try and do something very similar to the TMongoWire demo program to load a document, I get a different result? On create of a page, I want to get all the company names and load them (eventually) into a combobox. This is the code I am playing with:
d:=BSON;
q:=TMongoWireQuery.Create(MongoWire);
try
q.Query('mwx1.companies',nil);
while q.Next(d) do
begin
s := BsonToJson(d);
sl.add(d['CompanyName']);
sl.Add(TBSONDocument(d).item['CompanyName'])
end;
finally
q.Free;
end;
At the point where I am assigning the JSON to S, the JSON is fully correct and the CompanyName has data in it. That line was there for testing only. But the next two lines both produce nothing (tried various ways to get the data) because it says that the value is null. If I inspect TBSONDocument(d) it shows all the correct information. So I am baffled as to why it isn't working. Any clues?
It starts out as an empty BSON, but when it's in the loop for the first iteration it contains the BSON of
'{"_id":"ObjectID(\"524547512dcf91c1dc7476cb\")","Email":"2324","CompanyName":"Test 1","Addr1":"fs","Addr2":"ertert","City":"iukuiuiku","State":"uikuiku","Zip":"dsf","Country":"ff","Phone":"fdsd","SalesPerson":"sds","ContactPhone":"sdf","ContactEmail":"fsdf","ContactName":"f","DateCreated":"g2","website":"34","SMS":"23","Logo":"23423","Status":"qwqw","Keywords":"3423","Shortcode":"qwqw","ParentID":"asda"}'
Related
Say I have multiple tables in {game} like {bullets}, where {bullets} has multiple tables as found below. How would I iterate through and call all the update functions contained in {game}?
--Below is a simplified example, Assume each table in {bullets} has multiple entries not just update. And that the final code must work in cases like game={bullets,coins,whatever}, each entry being of similar nature to bullets.
game={}
game.bullets={{update=function(self) end,...}, {update=function(self) end,...},...}
for obj in all(game) do
for things in all(obj) do
things:update() end end
--I'm not sure what I"m doing wrong and whether I even need a double for-loop.
--if bullets wasn't embedded in {game} it would just be:
for obj in all(bullets) do
obj:update()
end
I've also tried:
for obj in all(game.bullets) do
obj:update()
end
*correction: this works, the problem I want solved though is to make this work if I have multiple tables like {bullets} in {game}. Thus the first attempt at double iterations which failed. So rather than repeat the above as many times as I have items in {game}, I want to type a single statement.
all() isn't a standard function in Lua. Is that a helper function you found somewhere?
Hard to tell without seeing more examples, or documentation showing how it's used, with expected return values. Seems to be an iterator, similar in nature to pairs(). Possibly something like this:
for key, value in pairs( game ) do
for obj in all( value ) do
obj :update()
end
end
I have a procedure on MySQL that returns two results and I need to show this on Delphi but I didn't find how to pass for each result.
Here is how appear on DBForge when I execute, I want this on delphi too, show Query1 and Query2 in a TTabControl.
How to go through this queries and get the name of the query like: Query1,Query2?
You don't say what DB interface lib you're using, like FireDAC, Zeos, or something else.
You're going to issue something like a dbxyz.ExecSQL() call and check for some results.
It sounds like you're expecting to get back multiple records in the result set. Using a TTabControl, you'd simply create a list of tabs like "Result-1", "Result-2", and so on, depending on how many records you get back. (They're results, not queries.) You could select the first one by default.
When another tab is clicked, use the control's TabIndex property to select the corresponding result from the result set, then format up the data in that result and display it in whatever format you're using below the tabs.
There's so little detail you've given that it's impossible to show any code for anything other than the tab control's OnChange handler that will use the TabIndex property to find the desired result set. But this is the overall approach I'd take for this using the TTabControl.
I solve the problem with the command
var
tab: TTabItem;
stringGrid: TStringGrid;
repeat
tab:= TTabItem.Create(nil);
tab.Parent:= tabcontrol1;
tab.Text:= query.Fields.Fields[0].FieldName; //the table name
stringGrid:= TStringGrid.Create(Self);
stringGrid.Parent:= tab;
stringGrid.Align:= TAlignLayout.Client;
for I := 1 to query.FieldCount-1 do
begin
stringGrid.AddObject(TStringColumn.Create(stringGrid));
stringGrid.Columns[i-1].Header:= query.Fields.Fields[i].FieldName;
query.First;
for j := 0 to query.RecordCount-1 do
begin
stringGrid.cells[i-1, j]:= query.Fields.Fields[i].value;
query.Next;
end;
end;
stringGrid.RowCount:= j;
until not query.openNext;
We have grids that we have custom filters for and you can save the filters as a JSON object to a particular table, tblUserPersistedValue (along with a user field).
So we are updating the page and the filters are going from single-select IDs to multi-select IDs on four different filters.
Curent JSON:
'{
"ProductGroupID":1,
"TerminalID":1,
"CarrierID":1221,
"RegionID":21,
"DriverDateFilter":0,
"DriverStartDate":"0001-01-01T00:00:00",
"DriverEndDate":"0001-01-01T00:00:00"
}';
Where You see "TerminalID":1, I would like to change it to "TerminalID":[1]
so we don't break any user's settings when we make the update.
Unfortunately i am unable to figure out how to create an int array with JSON_MODIFY.
I am almost there, but it's wrapping the value i'm saving in quotes, and that won't parse into an int array during deserialization.
I have never worked with json directly in sql before and I must be missing something simple. How do i fix it?
If it's impossible to overwrite the existing entry, I could also make a new TerminalIDs entry to parse into and remove the old one.
The ultimate goal is to do this for every record in the table, once for each of four fields.
declare #j nvarchar(max) = N'{"ProductGroupID":1,"TerminalID":1,"CarrierID":1221,"RegionID":21,"DriverDateFilter":0,"DriverStartDate":"0001-01-01T00:00:00","DriverEndDate":"0001-01-01T00:00:00"}';
select #j = json_modify(#j, '$.TerminalID', json_query(concat('[', json_value(#j, '$.TerminalID') ,']')));
select #j;
Searched everywhere but couldn't find something similar to this.
I got a table as below (a glimpse of it):
customDimensions is the column that pulls the data in JSON format. However, there are some empty cells (as seen below) in the column.
I tried to Parse the whole column into JSON but it is returning me errors in the blank columns as below:
Therefore I tried an alternative way of first replacing those blank cells in the customDimensions to a generic JSON format input as {"Type":"Unhandled"} and then Parsed the column into JSON. This successfully got rid of the errors, but it is returning error while refreshing the data!
So, How do I handle this in a different way? I want to replace those blank cells as either "null" or any keyword, as long as I do not miss the count of those blank cells.
EDIT
Re: This successfully got rid of the errors, but it is returning error while refreshing the data! The error is as follows:
And that specific error is as above
EDIT 2:
Got trapped in the XY Problem but actually you can just replace the error after parsing the JSON instead:
Repeat with all columns and it'll be fine.
I know how to parse json cells in Open refine, but this one is too tricky for me.
I've used an API to extract the calendar of 4730 AirBNB's rooms, identified by their IDs.
Here is an example of one Json file : https://fr.airbnb.com/api/v2/calendar_months?key=d306zoyjsyarp7ifhu67rjxn52tv0t20¤cy=EUR&locale=fr&listing_id=4212133&month=11&year=2016&count=12&_format=with_conditions
For each ID and each day of the year from now until november 2017, i would like to extract the availability of this rooms (true or false) and its price at this day.
I can't figure out how to parse out these informations. I guess that it implies a series of nested forEach, but i can't find the right way to do this with Open Refine.
I've tried, of course,
forEach(value.parseJson().calendar_months, e, e.days)
The result is an array of arrays of dictionnaries that disrupts me.
Any help would be appreciate. If the operation is too difficult in Open Refine, a solution with R (or Python) would also be fine for me.
Rather than just creating your Project as text, and working with GREL to parse out...
The best way is just select the JSON record part that you want to work with using our visual importer wizard for JSON files and XML files (you can even use a URL pointing to a JSON file as in your example). (A video tutorial shows how here: https://www.youtube.com/watch?v=vUxdB-nl0Bw )
Select the JSON part that contains your records that you want to parse and work with (this can be any repeating part, just select one of them and OpenRefine will extract all the rest)
Limit the amount of data rows that you want to load in during creation, or leave default of all rows.
Click Create Project and now your in Rows mode. However if you think that Records mode might be better suited for context, just import the project again as JSON and then select the next outside area of the content, perhaps a larger array that contains a key field, etc. In the example, the key field would probably be the Date, and why I highlight the whole record for a given date. This way OpenRefine will have Keys for each record and Records mode lets you work with them better than Row mode.
Feel free to take this example and make it better and even more helpful for all , add it to our Wiki section on How to Use
I think you are on the right track. The output of:
forEach(value.parseJson().calendar_months, e, e.days)
is hard to read because OpenRefine and JSON both use square brackets to indicate arrays. What you are getting from this expression is an OR array containing twelve items (one for each month of the year). The items in the OR array are JSON - each one an array of days in the month.
To keep the steps manageable I'd suggest tackling it like this:
First use
forEach(value.parseJson().calendar_months,m,m.days).join("|")
You have to use 'join' because OR can't store OR arrays directly in a cell - it has to be a string.
Then use "Edit Cells->Split multi-valued cells" - this will get you 12 rows per ID, each containing a JSON expression. Now for each ID you have 12 rows in OR
Then use:
forEach(value.parseJson(),d,d).join("|")
This splits the JSON down into the individual days
Then use "Edit Cells->Split multi-valued cells" again to split the details for each day into its own cell.
Using the JSON from example URL above - this gives me 441 rows for the single ID - each contains the JSON describing the availability & price for a single day. At this point you can use the 'fill down' function on the ID column to fill in the ID for each of the rows.
You've now got some pretty easy JSON in each cell - so you can extract availability using
value.parseJson().available
etc.