how to parse json from server response? - ios

I am using AFNetworking for web API calls.
i am able to call the API and able to receive the response from server.
but m not able to parse the json.
following is the response from server:
<string xmlns="http://tempuri.org/">
{ "ExistsInDB": "False", "CanSave": "True", "EntityName": "ACCOUNT", "TypeDescription":
"Company", "TypePluralDescription": "Companies", "RequiredProperties":
"SalesAchiever.Data.SAQueryFieldSet", "MetaData": "", "ReadOnly": "False", "ACCTNAME":
"", "AREA_ID": "", "ACCT_TYPE_ID": "", "ADDR1": "", "ADDR2": "", "ADDR3": "", "TOWN":
"", "COUNTY": "", "POSTCODE": "", "COUNTRY": "", "TEL": "", "FAX": "", "EMAILORWEB":
"", "BUYGRP_ID": "", "STATUS": "", "SIC_CODE_ID": "", "CURRENCY_ID": "", "CALL_FREQ":
"0", "DORMANT": "False", "CREATOR_ID": "", "CREATED_ON": "01/01/0001 00:00:00",
"LAST_EDITOR_ID": "", "LAST_EDITED": "01/01/0001 00:00:00", "LAST_ACTION_BY": "",
"LAST_ACTION": "01/01/0001 00:00:00", "NEXT_ACTION_BY": "" }
</string>
my problem is i am able to fetch the response from server.But the response contains some header file. i.e <string>....data</string>.
I want to remove that header file from the response and save into NSDictionary
can any one please help me out with this?
i am new to this platform.
Thanks in Advance.

Add the header file in the server:
For example, if you are using PHP, add this:
header('Content-Type: application/json');

Related

Process json result from Twilio

I currently retrieve the alerts for voice and sms messages from Twilio as a json column. using this tsql code,
SELECT [ReceivedDateTime]
,MsgSid = JSON_VALUE(payload, '$.resource_sid')
,WebhookURL = JSON_VALUE(payload, '$.webhook.request.url')
,ToState = JSON_VALUE(payload, '$.webhook.request.parameters.ToState')
,AlertTime = JSON_VALUE(payload, '$.webhook.request.parameters.Timestamp')
,Direction = JSON_VALUE(payload, '$.webhook.request.parameters.Direction')
,SequenceNumber = JSON_VALUE(payload, '$.webhook.request.parameters.SequenceNumber')
,ToNumber = JSON_VALUE(payload, '$.webhook.request.parameters.To')
,ToCity = JSON_VALUE(payload, '$.webhook.request.parameters.ToCity')
,CallStatus = JSON_VALUE(payload, '$.webhook.request.parameters.CallStatus')
,FromNumber = JSON_VALUE(payload, '$.webhook.request.parameters.From')
,AccountSid = JSON_VALUE(payload, '$.webhook.request.parameters.AccountSid')
,TwilioReason = JSON_VALUE(payload, '$.response.headers.X-Twilio-Reason')
,AlertDate = JSON_VALUE(payload, '$.webhook.response.headers.Date')
FROM [dbo].[TwilioAlerts]
I can query this file until I get to the section that contains a dash in the name. Trying to pull in the X-Twilio-Reason, I get the error: JSON path is not properly formatted. Unexpected character '-' is found at position 20. How can I reference this in tsql?
This is the json I trying to query:
{
"resource_sid": "",
"service_sid": null,
"error_code": "15003",
"more_info": {
"Msg": "HTTP retrieval failure",
"statusCallback": "",
"ErrorCode": "15003",
"LogLevel": "WARN"
},
"webhook": {
"type": "application/json",
"request": {
"url": "",
"method": "POST",
"headers": {},
"parameters": {
"Called": "+",
"ToState": "TX",
"CallerCountry": "US",
"Timestamp": "Fri, 22 Nov 2019 14:33:23 +0000",
"Direction": "outbound-api",
"CallbackSource": "call-progress-events",
"CallerState": "TX",
"ToZip": "78742",
"SequenceNumber": "1",
"CallSid": "",
"To": "+",
"CallerZip": "",
"ToCountry": "US",
"CalledZip": "",
"ApiVersion": "2010-04-01",
"CalledCity": "",
"CallStatus": "ringing",
"From": "+",
"AccountSid": "",
"CalledCountry": "US",
"CallerCity": "",
"ToCity": "AUSTIN",
"Caller": "+",
"FromCountry": "US",
"FromCity": "",
"CalledState": "TX",
"FromZip": "",
"FromState": "TX"
}
},
"response": {
"status_code": null,
"headers": {
"X-Twilio-WebhookAttempt": "1",
"X-Twilio-Reason": "Response does not contain content type",
"Content-Length": "464",
"Date": "Fri, 22 Nov 2019 14:33:23 GMT",
"Content-Type": "text/html"
},
"body":"Twilio was unable to fetch content from: https://appointmentwave.com/ProdAWHandler/events/voice\nError: Error reading response: Response does not contain content type\nAccount SID: ACc9eea08b61d774d5dfc07c91aa690466\nSID: CA6e2b878c4e1e5d2ec3733d52b2c2b2fa\nRequest ID: 6ae4e44b-f6cb-4ca3-a057-6067024f943a\nRemote Host: appointmentwave.com\nRequest Method: POST\nRequest URI: https://appointmentwave.com/ProdAWHandler/events/voice\nSSL
Version: TLSv1.2\nURL Fragment: true"}}}
Any answer as to how to pull "X-Twilio-Reason" ?
Try adding double-quotes around the section with dashes in the name.
,TwilioReason = JSON_VALUE(payload, '$.response.headers."X-Twilio-Reason"')

How to convert string array into array and remove empty elements?

I have a string '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'. i want to convert it into array and remove empty values from that array. my desired output is abc;def;mno.
Can someone help me to do this?
You could use JSON.parse and select method:
str = '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'
arr = JSON.parse(str).select(&:present?)
Output array: ["abc", "def", "mno"]
If you want to get abc;def;mno:
joined = arr.join(';')
Output string: "abc;def;mno"
Hope this helps
Use this code:
str = YAML.load('["", "abc", "", "def", "", "mno", "", "", "", "", ""]')
str.select{|a| a if a != ""}.join(";")
You can parse your string with JSON#parse and use delete with join:
str = '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'
JSON.parse(str).tap { |arr| arr.delete('') }.join(';')
# => "abc;def;mno"

Accessing Data Members after using expand in Odata

I currently have an odata service with 2 entity sets. I am using expand to navigate to second entity set, and binding values from both in a table.
However, I am unable to bind data from second entity set as {to_PurchaseOrderItem/PurchaseOrderQuantityUnit}
Am I doing this wrong?
The JSON reply looks something like this, so I am really confused what I am doing wrong.
"d": {
"results": [
{
"PurchaseOrder": "4500000001",
"Language": "EN",
"PaymentTerms": "0004",
"CashDiscount1Days": "0",
"CashDiscount2Days": "0",
"NetPaymentDays": "0",
"CashDiscount1Percent": "0.000",
"CashDiscount2Percent": "0.000",
"PurchasingOrganization": "1710",
"PurchasingDocumentOrigin": "9",
"PurchasingGroup": "001",
"CompanyCode": "1710",
"PurchaseOrderDate": "/Date(1468454400000)/",
"DocumentCurrency": "USD",
"ExchangeRate": "1.00000",
"ValidityStartDate": null,
"ValidityEndDate": null,
"SupplierQuotationExternalID": "",
"SupplierRespSalesPersonName": "",
"SupplierPhoneNumber": "",
"SupplyingSupplier": "",
"SupplyingPlant": "",
"PurchaseOrderType": "NB",
"IncotermsClassification": "",
"InvoicingParty": "17300001",
"ReleaseIsNotCompleted": false,
"PurchasingCompletenessStatus": false,
"IncotermsVersion": "",
"IncotermsLocation1": "",
"IncotermsLocation2": "",
"ManualSupplierAddressID": "",
"AddressCityName": "",
"AddressFaxNumber": "",
"PurchasingDocumentDeletionCode": "",
"AddressHouseNumber": "",
"AddressName": "",
"AddressPostalCode": "",
"AddressStreetName": "",
"AddressPhoneNumber": "",
"AddressRegion": "",
"AddressCountry": "",
"AddressCorrespondenceLanguage": "",
"PurchasingProcessingStatus": "02",
"CreatedByUser": "CB9980000025",
"CreationDate": "/Date(1468454400000)/",
"Supplier": "17300001",
"PurchaseOrderSubtype": "",
"to_PurchaseOrderItem": {
"results": [
{
"PurchaseOrder": "4500000001",
"OrderQuantity": "100",
"PurchaseOrderQuantityUnit": "ES",
"OrderPriceUnit": "ES",
"OrderPriceUnitToOrderUnitNmrtr": "1",
"OrdPriceUnitToOrderUnitDnmntr": "1",
"NetPriceAmount": "0.21",
"DocumentCurrency": "USD",
"NetPriceQuantity": "1",
"TaxCode": "",
"PriceIsToBePrinted": true,
"PurchaseOrderItem": "1",
"OverdelivTolrtdLmtRatioInPct": "10.0",
"UnlimitedOverdeliveryIsAllowed": false,
"UnderdelivTolrtdLmtRatioInPct": "10.0",
"ValuationType": "",
"IsCompletelyDelivered": false,
"IsFinallyInvoiced": false,
"PurchaseOrderItemCategory": "0",
"AccountAssignmentCategory": "",
"MultipleAcctAssgmtDistribution": "",
"PartialInvoiceDistribution": "",
"PurchasingDocumentDeletionCode": "L",
"GoodsReceiptIsExpected": true,
"GoodsReceiptIsNonValuated": false,
"InvoiceIsExpected": true,
"InvoiceIsGoodsReceiptBased": false,
"PurchaseContract": "",
"PurchaseContractItem": "0",
"Customer": "",
"ItemNetWeight": "0.000",
"ItemWeightUnit": "",
"TaxJurisdiction": "0508525201",
"PurchaseOrderItemText": "RAW15,PD",
"PricingDateControl": "",
"ItemVolume": "0.000",
"ItemVolumeUnit": "",
"SupplierConfirmationControlKey": "",
"IncotermsClassification": "EXW",
"IncotermsTransferLocation": "VENDOR",
"EvaldRcptSettlmtIsAllowed": false,
"PurchaseRequisition": "",
"PurchaseRequisitionItem": "0",
"IsReturnsItem": false,
"Plant": "1710",
"RequisitionerName": "",
"ServicePackage": "0",
"EarmarkedFunds": "",
"EarmarkedFundsItem": "0",
"IncotermsLocation1": "VENDOR",
"IncotermsLocation2": "",
"Material": "RM15",
"ManufacturerMaterial": "RM15",
"ServicePerformer": "",
"ProductType": "1",
"StorageLocation": "",
"DeliveryAddressID": "",
"DeliveryAddressName": "",
"DeliveryAddressStreetName": "",
"DeliveryAddressHouseNumber": "",
"DeliveryAddressCityName": "",
"DeliveryAddressPostalCode": "",
"DeliveryAddressRegion": "",
"DeliveryAddressCountry": "",
"MaterialGroup": "L002",
"PurchasingInfoRecord": "5300000201",
"SupplierMaterialNumber": ""
I am trying to access Order Quantity to be specific.
Thanks!
I think you are missing "results" while binding the control. Please see below and try
{to_PurchaseOrderItem/results/0/PurchaseOrderQuantityUnit}

How to send this type of parameter to server call in swift 3.0

I need to send this below parameters to a server in swift 3.0. How can I build the request parameters?
"step_1": [
{
"name": "",
"mobile": "",
"line_1": "",
"line_2": "",
"city": "",
"state": "",
"pincode": "",
"fax": "",
"gst": "",
"owner_name": "",
"dob": "",
"anniversary": ""
}
]
For upper json data below is Dictionary code:
let param = [["name": "abc",
"mobile": "123",
"line_1": "line_1",
"line_2": "line_2",
"city":"xyz",
"state":"abc",
"pincode":"000",
"fax":"123",
"gst":"gst",
"ownwer_name":"ownwer_name",
"dob":"xx-xx-xxxx",
"anniversary":"xx-xx-xxxx"]]
//Print data
print(param[0]["name"] as! String)
Hi I'm not sure if it will work, but anyway, try this
let params: ["step_1": [["name": "Some name",
"mobile": "12345678",
"line_1": "line_1 stuff",
"...": "..."]]]

Whats Wrong with my Cypher FOREACH?

Can some one please help me understand whats wrong with my Cypher query
MATCH (sys:System{Name:'MYSYS' , Version:'1.0'})
FOREACH (item in [
{
itemObj: {
ItemID: "DOMAIN",
Name: "DOMAIN",
TypeID: "",
Physical: "",
Logical: ""
},
parameters: {
VERSIONNUM: "",
QUALSTATE: "",
VERSION: "",
CREATEDBY: "",
DATECREATED: "",
MODIFIEDBY: "",
VERSIONDATE: "",
BLCKCOMMENT1: "",
BLCKCOMMENT2: "",
BLCKCOMMENT3: "",
BLCKCOMMENT4: "",
ORIGASSIGN: "",
CANETYPE: "",
FULLITEMNAME: "",
DESC: "",
SCANPNTDTL: "",
SCANASSOCDSP: "",
ADSIPATH: "",
ASSIGNABLE: ""
}
},
{
itemObj: {
ItemID: "COMPUTERS",
Name: "COMPUTERS",
TypeID: "",
Physical: "",
Logical: ""
},
parameters: {
VERSIONNUM: "",
QUALSTATE: "",
VERSION: "",
CREATEDBY: "",
DATECREATED: "",
MODIFIEDBY: "",
VERSIONDATE: "",
BLCKCOMMENT1: "",
BLCKCOMMENT2: "",
BLCKCOMMENT3: "",
BLCKCOMMENT4: "",
ORIGASSIGN: "",
CANETYPE: "",
FULLITEMNAME: "",
DESC: "",
SCANPNTDTL: "",
SCANASSOCDSP: "",
ASSIGNABLE: ""
}
},
{
itemObj: {
ItemID: ".htm",
Name: ".htm",
TypeID: "",
Physical: "",
Logical: ""
},
parameters: {
FileName: ""
}
}
] |
CREATE (p:Parameter) SET p=item.parameters
MERGE (it:Item { ItemID: item.itemObj.ItemID}) ON CREATE SET it=item.itemObj
MERGE (sys)-[HAS_ITEM]->(it)-[:HAS_PARAMETERS]->(p) )
I end up getting this error
A single relationship type must be specified for CREATE (line 77, column 12)
I am trying to learn to use foreach as its seams very fast in case of bulk insert, so any help here is really welcome.
Regards
Kiran
You are missing a : when declaring relationship type. On the last line, [HAS_ITEM] should be [:HAS_ITEM].

Resources