I'm trying to extract a specific key from JSON. I am using the Stripe API in combination with Firebase Cloud functions.
I am printing the data using:
print(success!["charges"])
This is the output:
data = (
{
amount = 4500;
"amount_captured" = 4500;
captured = 1;
created = 1610732200;
currency = gbp;
description = "<null>";
dispute = "<null>";
disputed = 0;
"failure_code" = "<null>";
"failure_message" = "<null>";
"fraud_details" = {
};
id = "ch_1I9wE0GX6DUCOIM8aadiMKBU";
invoice = "<null>";
livemode = 0;
metadata = {
};
object = charge;
})
I can get the charges section of the response, but I need to access the id key within the data array. I just need the simple value, I don't need to save it, I just need to it to pass as a parameter.
Related
I am trying to create a group with the following dot.net code:
var groupDef = new Group()
{
DisplayName = name,
MailNickname = name + " " + GetTimestamp(),
Description = "Group/Team created for testing purposes",
Visibility = "Private",
GroupTypes = new string[] { "Unified" }, // same for all teams
MailEnabled = true, // same for all teams
SecurityEnabled = false, // same for all teams
AdditionalData = new Dictionary<string, object>()
{
["owners#odata.bind"] = owners.Select(o => $"{graphV1Endpoint}/users/{o.Id}").ToArray(),
["members#odata.bind"] = members.Select(o => $"{graphV1Endpoint}/users/{o.Id}").ToArray(),
}
};
// Create the modern group for the team
Group group = await graph.Groups.Request().AddAsync(groupDef);
I am getting a "Method not allowed." error thrown on the last line shown (Group group = await ...).
The scope parameter for the auth provider contains "Group.Read.All Group.ReadWrite.All".
If I add Group.Create to the scope I get an error stating the scope is invalid. Reducing the scope to just "Group.Create" also gives an error.
It certainly appears that I cannot create a group without Group.Create in the scope, but that throws an error at sign in.
Microsoft.Graph is version 3.19.0
Microsoft.Graph.Core is version 1.22.0
I ended up serializing the object and making the Http call with my own code. Basically, something like this:
string json = JsonConvert.SerializeObject(groupDef, jsonSettings);
Group group = HttpPost<Group>("/groups", json);
No permissions were changed.
My C# MVC5 Razor page returns a Newtonsoft json link object to my controller (the "1" before \nEdit |" indicates that the checkbox is checked:
"{\"0\": [\"6146\",\"Kimball\",\"Jimmy\",\"General Funny Guy\",\"277\",\"Unite\",\"Jun 2019\",\"\",\"\",\"1\",\"\nEdit |\nDetails\n\n \n\"],\"1\": [\"6147\",\"Hawk\",\"Jack\",\"\",\"547\",\"Painters\",\"Jun 2019\",\"\",\"\",\"on\",\"\nEdit |\nDetails\n\n \n\"]}"
How do I parse this?
I am using a WebGrid to view and I want to allow the users to update only the lines they want (by checking the checkbox for that row), but it doesn't include an id for the 's in the dom. I figured out how to pull the values, but not the fieldname: "Last Name" , value: "Smith"... I only have the value and can't seem to parse it... one of my many failed attempts:
public ActoinResult AttMods(string gridData)
{
dynamic parsedArray = JsonConvert.DeserializeObject(gridData);
foreach (var item in parsedArray)
{
string[] itemvalue = item.Split(delimiterChars);
{
var id = itemvalue[0];
}
}
I finally sorted this one out..If there is a more dynamic answer, please share... I'll give it a few days before I accept my own (admittedly clugy) answer.
if(ModelState.IsValid)
{
try
{
Dictionary <string,string[]> log = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string[]>>(gridData);
foreach (KeyValuePair<string,string[]> keyValue in log)
{
if (keyValue.Value[9] == "1")//Update this row based on the checkbox being checked
{
var AttendeeID = keyValue.Value[0];
int intAttendeeID = 0;
if (int.TryParse(AttendeeID, out intAttendeeID))//Make sure the AttendeeID is valid
{
var LName = keyValue.Value[1];
var FName = keyValue.Value[2];
var Title = keyValue.Value[3];
var kOrgID = keyValue.Value[4];
var Org = keyValue.Value[5];
var City = keyValue.Value[7];
var State = keyValue.Value[8];
var LegalApproval = keyValue.Value[9];
tblAttendee att = db.tblAttendees.Find(Convert.ToInt32(AttendeeID));
att.FName = FName;
att.LName = LName;
att.Title = Title;
att.kOrgID = Convert.ToInt32(kOrgID);
att.Organization = Org;
att.City = City;
att.State = State;
att.LegalApprovedAtt = Convert.ToBoolean(LegalApproval);
}
}
}
db.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
You can avoid assigning the var's and just populate the att object with the KeyValue.Value[n] value, but you get the idea.
How can I convert the response message that returned from using bloomberg API in (python3 and flask) in to JSON
here is a response example:
ReferenceDataResponse = {
securityData[] = {
securityData = {
security = "FB EQUITY"
eidData[] = {
}
fieldExceptions[] = {
}
sequenceNumber = 0
fieldData = {
PX_LAST = 186.270000
VOLUME = 16746904.000000
}
}
securityData = {
security = "IBM EQUITY"
eidData[] = {
}
fieldExceptions[] = {
}
sequenceNumber = 1
fieldData = {
PX_LAST = 134.400000
VOLUME = 2551009.000000
}
}
}
}
dealing with it with the comming piece of code :
if str(msg.messageType()) == "ReferenceDataResponse":
securities = msg.getElement('securityData')
securities_count = securities.numValues()
for i in range(securities_count):
security = securities.getValueAsElement(i)
ticker = security.getElementAsString('security')
if (security.hasElement('fieldData')):
fields = security.getElement('fieldData')
fields_count = fields.numElements()
for j in range (fields_count):
security_dict = None
field = fields.getElement(j)
f_name = field.name()
f_value = field.getValueAsString()
security_dict = {"ticker":ticker ,"f_name":f_name , "f_value":f_value}
bloom_data.append(security_dict)
give me (Object of type Name is not JSON serializable)
now, I cant not access the name object to reach the name of the fields
any help will be very appreciated
After a lot of search I found this doc that is very helpful for as a schema for using the bloomberg api for dealing with the response ....
Here's the link ==> api schema
example for handeling respnse using python3:
bloom_data = []
if str(msg.messageType()) == "ReferenceDataResponse":
securities = msg.getElement('securityData')
securities_count = securities.numValues()
for i in range(securities_count):
security = securities.getValueAsElement(i)
ticker = security.getElementAsString('security')
if (security.hasElement('fieldData')):
fields = security.getElement('fieldData')
fields_count = fields.numElements()
for j in range (fields_count):
security_dict = None
field = fields.getElement(j)
f_name = field.name()
f_value = field.getValueAsString()
security_dict = {"ticker":ticker ,"f_name":str(f_name) , "f_value":f_value}
bloom_data.append(security_dict)
This is my response ... My dictnary name is "msg"
{
Result = {
Data = {
engine = {
service = {
data = (
{
checked = 1;
field = 1;
name = Ettersyn;
},
{
checked = 1;
field = 1;
name = "Med Alpha VP";
},
{
checked = 0;
field = 0;
name = "Med B.IMP.";
}
);
"engine_service_id" = 1;
"engine_service_name" = "4 Cyl";
};
type = {
"engine_id" = 1;
"engine_name" = Benzin;
};
};
tact = {
service = {
data = (
{
checked = 0;
field = 1;
name = "Little ettersyn";
},
{
checked = 0;
field = 1;
name = "Short ettersyn";
}
);
"tact_service_id" = 2;
"tact_service_name" = "21-50 hk";
};
type = {
"tact_id" = 1;
"tact_name" = "2 Takt";
};
};
};
status = success;
};
}
I try to to store the data like this
var arrayMain : NSMutableArray = msg.valueForKey("Result")?.valueForKey("Data") as NSMutableArray
My problem is i got "EXC_BAD_ACCESS" error While running ... Am also check allocation of array ... I try to store it in NSMutableDictionary its work perfectly ... How can i store this response in NSMutableArray ?
If you look carefully at the response data structure you'll see the problem.
This first operation: msg.valueForKey("Result") would return an dictionary with two key/values; a "Data" dictionary, and "Status" string.
Your second operation .valueForKey("Data") would return a dictionary with two key/values; an "Engine and "Tact" dictionary.
Because it's a dictionary, you can create a dictionary from it, which is what you said worked, or you're going to have to rethink how you want to store if if indeed you want it in an array, but you can't create an array directly from a dictionary - they are different data structures.
You cannot assign Dictionary object to Array, that's why it is giving the error.
But you can store that dictionary to array as:
var arrayMain : NSMutableArray = NSMutableArray()
arrayMain.addObject(msg.valueForKey("Result")?.valueForKey("Data"))
I am getting the following data in JSON format by the Facebook graph API. However it looks like it is in invalid JSON format. The JSON formatter says that the error is in line 1. What is this error ?
{
data = (
{
application = {
id = 2409997254;
name = Likes;
};
"created_time" = "2013-05-05T07:51:41+0000";
from = {
id = 100000347121257;
name = "Rishab Gulati";
};
id = "notif_746853089_182660043";
link = "http://www.facebook.com/photo.php?fbid=10151457234013090&set=a.427064503089.223857.746853089&type=1";
object = "<null>";
title = "Rishab Gulati, Deepika Gurnani and 2 other people like your photo.";
to = {
id = 746853089;
name = "Ashish Agarwal";
};
unread = 1;
"updated_time" = "2013-05-05T10:48:33+0000";
}
);
paging = {
next = "https://graph.facebook.com/746853089/notifications?format=json&access_token=**xxxxxxxxxx";
previous = "https://graph.facebook.com/746853089/notifications?format=json&access_token=**SNIP**&limit=5000&since=1367740301&__paging_token=notif_746853089_182660043&__previous=1";
};
summary = {
"unseen_count" = 1;
"updated_time" = "2013-05-05T10:48:33+0000";
};
}
This data is incorrect and isn't what Facebook supplies
{
"data": [
{
Where as your JSON above shows
{
data = (
{
There is a ( when there should be a {
Also consider not giving your access token out in public.