{
"_id" : ObjectId("4ccb42cb8aad692e01000004"),
"loc" : {
"lat" : 37.799506,
"long" : -122.459445
},
"test_set" : 1,
"title" : "Melissa Mills Housewife 01 SIGNED",
"num_comments" : 58,
"down_votes" : 66,
"up_votes" : 79,
"image_url" : "http://farm2.static.flickr.com/1374/5126544615_79170591e5_m.jpg",
"image_url_thumb" : "http://farm2.static.flickr.com/1374/5126544615_79170591e5_t.jpg",
"date" : "Fri Oct 29 2010 21:55:23 GMT+0000 (UTC)",
"flickr_id" : "5126544615"
}
One of the elements in thelist is above.
thejson = simplejson.dumps({"results":thelist})
However, I can't serialize this because of the date field. It can't serialize datetime.
I doubt that the problem has to do anything with datetime: in your dictionary, there is no datetime object at all, but the "date" key has a regular string value.
More likely, the problem is that it can't serialize the ObjectId class. To overcome this limitation, create a new class inheriting from JSONEncoder, and overriding the default method.
Unless i'm missing something - its the ObjectId that is causing the error (works for me here without it). You might want to consider munging or removing that field if not needed.
The date parses fine.
This works for me. I have removed ObjectId as I do not have the class with me.
result = {
"loc" : {
"lat" : 37.799506,
"long" : -122.459445
},
"test_set" : 1,
"title" : "Melissa Mills Housewife 01 SIGNED",
"num_comments" : 58,
"down_votes" : 66,
"up_votes" : 79,
"image_url" : "http://farm2.static.flickr.com/1374/5126544615_79170591e5_m.jpg",
"image_url_thumb" : "http://farm2.static.flickr.com/1374/5126544615_79170591e5_t.jpg",
"date" : "Fri Oct 29 2010 21:55:23 GMT+0000 (UTC)",
"flickr_id" : "5126544615"
}
import simplejson
thejson = simplejson.dumps(result)
print thejson
Output:
{"down_votes": 66, "loc": {"lat": 37.799506000000001, "long": -122.459445}, "image_url": "http://farm2.static.flickr.com/1374/5126544615_79170591e5_m.jpg", "test_set": 1, "title": "Melissa Mills Housewife 01 SIGNED", "up_votes": 79, "num_comments": 58, "image_url_thumb": "http://farm2.static.flickr.com/1374/5126544615_79170591e5_t.jpg", "date": "Fri Oct 29 2010 21:55:23 GMT+0000 (UTC)", "flickr_id": "5126544615"}
And if you are getting the following error, then you need to have class ObjectId :
"_id" : ObjectId("4ccb42cb8aad692e01000004"),
NameError: name 'ObjectId' is not defined
Related
hi i have an JSON object like this :
WorkTime=[{
"Day": "6",
"Time1": "8 : 00 - 9 : 00",
"Time2": "9 : 00 - 10 : 00",
"Time3": "10 : 00 - 11 : 00",
"Time4": "11 : 00 - 12 : 00",
"Time5": "12 : 00 - 13 : 00",
"Time6": "13 : 00 - 14 : 00",
"Time7": "17 : 00 - 18 : 00",
"Time8": "18 : 00 - 19 : 00",
"Time9": "19 : 00 - 20 : 00",
"id": 1
},
{
"Day": "0",
"Time1": "8 : 00 - 9 : 00",
"Time2": "9 : 00 - 10 : 00",
"Time3": "10 : 00 - 11 : 00",
"Time4": "11 : 00 - 12 : 00",
"Time5": "12 : 00 - 13 : 00",
"Time6": "13 : 00 - 14 : 00",
"id": 2
},]
and i want insert object's Value into My div . but i should write the Object key and my Number of Keys is Variable. for this i decide to use index and dynamic call Keys but i don't know anything about its Syntax .
What I want to do is almost like this :
{
WorkTime.map((item, index) => (
<div key={index} id={`Ticket${item.Day}Box`} className="bg-gray-50 p-3 w-11/12
rounded-t-xl mx-auto" style={{ display: "none" }}>
{item.Time{index+1}}
</div>
)) }
instead Of Call Keys Like This :
WorkTime.map((item, index) => (
<div key={index} id={`Ticket${item.Day}Box`} className="bg-gray-50 p-3 w-11/12
rounded-t-xl mx-auto" style={{ display: "none" }}>
{item.Time1}
</div>
)) }
To answer your question, you can use the brackets syntax and string interpolation to get what you want:
item[`Time${index + 1}`]
I'm assuming you do realize that this will only give you the first time entry from every WorkTime object. If you want to show all the time entries, you'll have to iterate over them separately. For this I'd recommend changing the structure of your json to something like this:
WorkTime = [{
id: 0, // whatever
day: "day string",
times: ["time 1", "time 2"] // etc
}]
It modifying json is not possible, then you can collect all the Time keys with this:
Object.keys(item).filter(k => k.match(/Time\d/))
I'm using rrule to create and store events in my database.
All was working until I found that my recurring events had an one hour difference past the 31st march.
In France, it's the day when we do a daylight saving time change.
Actually, my events are stored in a mongo database, with the start date and the duration of the event, + the eventuals rrules (all events aren't recurring events) like this :
{
"_id" : ObjectId("5c8e4706703df43859aabbe7"),
"duration" : 2879,
"type" : "unavailability",
"title" : "Weekend",
"description" : "C'est le weekend",
"rrules" : [
{
"until" : ISODate("2021-03-22T23:00:00.000Z"),
"dtstart" : ISODate("2019-03-11T23:00:00.000Z"),
"byweekday" : [
{
"weekday" : 5
},
{
"weekday" : 6
}
],
"interval" : 1,
"freq" : 2
}
],
"__v" : 0
}
When the frontend search for a date in the calendar, it will search with this args :
?from=2019-03-10T23:00:00.000Z&to=2019-03-17T23:00:00.000Z
It works well with this date, because no daylight savings are occuring in between. If I have this object :
normalizedDates = { from: 2019-03-10T23:00:00.000Z, to: 2019-03-17T23:00:00.000Z }
and this rule :
{ until: 2021-03-22T23:00:00.000Z,
dtstart: 2019-03-11T23:00:00.000Z,
byweekday: [ { weekday: 5 }, { weekday: 6 } ],
interval: 1,
freq: 2 }
Running :
const recurringDays = rruleSet.between(normalizedDates.from, normalizedDates.to)
shows, indeed :
recurringDays [ 2019-03-23T23:00:00.000Z ]
But if y use :
normalizedDates = { from: 2019-03-31T22:00:00.000Z, to: 2019-04-07T22:00:00.000Z }
Rrules returns :
recurringDays [ 2019-03-31T23:00:00.000Z, 2019-04-06T23:00:00.000Z ]
while I'm expecting :
recurringDays [ 2019-04-06T22:00:00.000Z ]
Do you know how I could handle this ?
If you want a recurrence rule to observe daylight saving time for a particular time zone, then you must schedule using this time zone. In your example, the schedule is based on UTC.
RRule provides time zone support. You should use that, and specify tzid: 'Europe/Paris'.
Also, you might consider using the toString and fromString functions to work with iCalendar formatted strings, and store that in your MongoDB instance instead of serializing the RRule as JSON.
I wanted to determine the datatype of a field in my JSON document in MongoDB. Is it possible to do so? Example if I have
{
"created_at": "Fri Aug 04 05:42:03 +0000 2017",
"id": 893346273255866400,
"id_str": "893346273255866368"
}
I wanted to know what type of values "created_at" accepts so I can formulate a query in my MongoDB
You can use the typeof operater to return the type of value in MongoDB and you can also query by a specific type by using $type operator in query.
e.g.
db.inventory.find( { price: { $type: "decimal" } } )
For more information see https://docs.mongodb.com/manual/core/shell-types/#check-types-in-shell
Here is data from MongoDB
{
"urls" : {
"beauty-credit" : {
"count" : 1,
"keyword" : "beauty credit",
"last_res" : 152,
"last_sea" : "Sat May 13 2017 15:16:41 GMT+0700 (SE Asia Standard Time)",
"url_site" : "beauty-credit"
},
"etude" : {
"count" : 2,
"keyword" : "etude",
"last_res" : 1048,
"last_sea" : "Sat May 13 2017 15:16:38 GMT+0700 (SE Asia Standard Time)",
"url_site" : "etude"
},
"skinfood" : {
"count" : 2,
"keyword" : "skinfood",
"last_res" : 478,
"last_sea" : "Sat May 13 2017 15:16:45 GMT+0700 (SE Asia Standard Time)",
"url_site" : "skinfood"
}
}
}
and Here is my code. Now I filter only last_res > 10
function gotData(data){
result = data.val()
const urls_kws = Object.keys(result)
.filter(key => result[key].last_res > 10)
}
How can I sort data by "count"? Also how can I show only 30 rows?
In order to sort the data by count you can use the sort() function from javascript with a custom comparator like
urls_kws.sort((a, b) => a.count - b.count)
In order to now get only the first 30 values, you can then slice the array
var short_urls_kws = urls_kws.slice(0, 30)
I have a variable that contains the following JSON string:
{
"0" : "Jun 20, 2012 03:02 PM",
"1" : "Jun 20, 2012 03:26 PM",
"2" : "Jun 21, 2012 01:12 PM",
"3" : "Jun 21, 2012 01:25 PM",
"4" : "Jun 21, 2012 02:42 PM",
"5" : "Jun 21, 2012 02:43 PM",
"6" : "NULL"
}
I wish to convert this JSON to an array in javascript such that
array[0] has "Jun 20, 2012 03:02 PM" array[1] has "Jun 20, 2012 03:26 PM" and so on.
You must parse your JSON string into a javascript object first.
JavaScript
var object = JSON.parse(JSONString);
To polyfill browsers without JSON support:
http://bestiejs.github.com/json3/
Then, convert that object to an array:
JavaScript
var arr = [];
for(var i in object) {
if(object.hasOwnProperty(i)) {
arr.push(object[i]);
}
}
jQuery
var arr = $.map(obj,function(value){ return value; });
Fiddle: http://jsfiddle.net/iambriansreed/MD3pF/
Note: Since the original poster did not mention jQuery it is worth mentioning that loading jQuery for only these instances isn't worthwhile, and you would be better off using the pure JavaScript if you aren't already using jQuery.
Alternatively, if you're targeting ES5 and above:
// myObject = { '0': 'a', '1': 'b' };
var myArray = Object.keys(myObject).map(function(key) { return myObject[key]; });
// myArray = [ 'a', 'b' ];
var currentVersion = {/literal} {$displayedVersion} {literal};
var jsonObj = eval('(' + {/literal}{$json}{literal} + ')');