How To Get Childs From Json Response Firebase - json

current_date = date.today()
result = firebase.get('/InstaFollow',None)
data = json.dumps(result,indent=2)
print(data)
Python
{
"User2Follow": {
"16_04_2020": {
"20:20:51": {
"Delightedtiktoks": "\"55822\""
},
"20:21:04": {
"Delightedtiktoks": "\"55822\""
},
"20:38:45": {
"Delightedtiktoks": "\"Testuser101\""
}
}
}
}
Hey Guys Im Making A Project With Python,And Im Stuck.
Im Using A realtime database from firerbase,this is my response(Json)
Im Trying To Get A List With The Dates "16_04_2020"(This JSON File only has 1 date,but there will be more )
Anyone Know How I Can Get A List With All Dates?
So if would run my script my output should be : "16_04_2020"

Try this.
import json
myjson = '''
{
"User2Follow": {
"16_04_2020": {
"20:20:51": {
"Delightedtiktoks": "55822"
},
"20:21:04": {
"Delightedtiktoks": "55822"
},
"20:38:45": {
"Delightedtiktoks": "Testuser101"
}
}
}
}'''
object = json.loads(myjson)
dates = [date for date in object['User2Follow']]
print(dates)
Output:
['16_04_2020']

Related

How to get an image url in this json file in flutter

in this json file I want to get original_ur but when i cant get it , can some one help me how to get it please
{
"orders": {
"order_items":[
{
"product_min":{
"colors":[
{
"media":[
{
"original_url": b
"https://royalpetiq.com/royalpet2/public//storage/10628/msg5212971698-890.jpg",
}
]
}
]
}
}
]
}
}
go on quicktype id site and create model from this json after just creat object of that claass and parse this json response and after that you can get valu like
x.orders.order_items[0].product_min.colors[0].media[0].original_url
here is your json path
x.orders.order_items[0].product_min.colors[0].media[0].original_url
try {
var json = """{
"orders": {
"order_items":[
{
"product_min":{
"colors":[
{
"media":[
{
"original_url": "https://royalpetiq.com/royalpet2/public//storage/10628/msg5212971698-890.jpg"
}
]
}
]
}
}
]
}
}""";
var data = jsonDecode(json);
var url = data["orders"]["order_items"][0]["product_min"]["colors"][0]
["media"][0]["original_url"];
print("Url: $url");
} catch (e) {
print(e);
}
I think your json should be like that.
Here the url has a b at the beginning and a comma after the url which is not the correct syntax for json

Get value of variable from json using jsonslurper

I have the following JSON code:
{
"TIMESTAMP":"2017-05-26-20.22.40.016000",
"dateTime":"2017-05-26H-20.22.4",
"AMUCCY1":"ADP",
"rates":[
{
"AMUCCY2":"AED",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"AFA",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"ALL",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"AMD",
"AMURAT":"1.000000000",
"AMUNXRT":0
}
]
}
Is there quick way in groovy where I could loop through each of the 'rates' and get the value of, let's say 'AMUCCY2' ?
I tried doing this code:
jsonObj.rates.each {
def toCurrencyMap = jsonObj.rates.AMUCCY2
LOG.info "${toCurrencyMap}"
}
but the toCurrencyMap returns an array of all four values of this field. I only want to get each value; not all.
Any suggestions is appreciated.
You can try this:
jsonObj.rates.each {
println it.AMUCCY2
}
If you want list / array:
def result = jsonObj.rates.collect { it.AMUCCY2 }
println result

Mongo: Move json string to a part of the document

I have a mongo collection where documents have aprox the following structure:
item{
data{"emailBody":
"{\"uniqueKey\":\" this is a stringified json\"}"
}
}
What I want to do is to use 'uniqueKey' as an indexed field, to make an "inner join" equivalant with items in a different collection.
I was thinking about running a loop on all the documents -> parsing the json -> Saving them as new property called "parsedEmailBody".
Is there a better way to handle stringified json in mongo?
The only way is to loop through the collection, parse the field to JSON and update the document in the loop:
db.collection.find({ "item.data.emailBody": { "$type": 2 } })
.snapshot().forEach(function(doc){
parsedEmailBody = JSON.parse(doc.item.data.emailBody);
printjson(parsedEmailBody);
db.collection.updateOne(
{ "_id": doc._id },
{ "$set": { "item.data.parsedEmailBody": parsedEmailBody } }
);
});
For large collections, leverage the updates using the Bulk API:
var cursor = db.collection.find({ "item.data.emailBody": { "$type": 2 } }).snapshot(),
ops = [];
cursor.forEach(function(doc){
var parsedEmailBody = JSON.parse(doc.item.data.emailBody);
ops.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": { "$set": { "item.data.parsedEmailBody": parsedEmailBody } }
}
});
if (ops.length === 500) {
db.collection.bulkWrite(ops);
ops = [];
}
});
if (ops.length > 0) { db.collection.bulkWrite(ops); }

coldfusion json parsing issue

I have the following json string and need to extract each of the survey_id values as a list. ie 74448500, 74052991, 65442357
{
"status":0,
"data":{
"surveys":[
{
"survey_id":"74448500"
},
{
"survey_id":"74052991"
},
{
"survey_id":"65442357"
}
],
"page":1,
"page_size":1000,
"metadata":{
"collaboration":{
"shared_by_total":0,
"unfiled_owned_total":143,
"shared_with_total":0,
"owned_total":242
}
}
}
}
Not sure what version of ColdFusion you are on, here are two possible ways to do it:
<cfscript>
x = deserializeJSON('{"status":0,"data":{"surveys":[{"survey_id":"74448500"},{"survey_id":"74052991"},{"survey_id":"65442357"}],"page":1,"page_size":1000,"metadata":{"collaboration":{"shared_by_total":0,"unfiled_owned_total":143,"shared_with_total":0,"owned_total":242}}}}');
// ColdFusion 11
y = x.data.surveys.map(function(item){
return item.survey_id;
});
writeDump(arrayToList(y));
// ColdFusion 9+
z = [];
for (item in x.data.surveys) {
arrayAppend(z, item.survey_id);
}
writeDump(arrayToList(z));
</cfscript>

Groovy to create JSON

Here's an SQL query I had to execute in Groovy:
def resultset_bio = sql.rows("SELECT author, isbn FROM Book WHERE genre = 'biography'")
I'm trying to convert this data into JSON. For that, I am using this code:
def json = new groovy.json.JsonBuilder()
json {
Biographies(resultset_bio.collect{[id: it]})
}
println json.toPrettyString()
}
The JSON output I expect should be like this:
{
"Biographies":
{
"SSS": ["XXX",456988]
}
}
But instead, I'm getting this:
{
"Biographies": [
{
"id": {
"author": "XXX",
"isbn": 456988,
}
}
]
}
How should I change my code? Please help.
Now id is passed as a static key.
Try:
json {
Biographies(resultset_bio.collect{[(it.id): it]})
}
You do not have the book title in your select so you can't make a mapping of title to book info.
In order to get the list layout of each row (which is a map) grouped by author:
def authorBios = resultset_bio.groupBy { it.author }
def biographies = authorBios.collectEntries { author, row ->
[author, row*.values()]
}
json {
Biographies(biographies)
}