Parse.Cloud.httpRequest inconsistency between data and text - json

I am running a Parse.Cloud.httpRequest to Google Geocoding API from Parse Cloud Code.
The request works fine. However, I am unable to retrieve the data.
Request:
var url_gg = 'https://maps.googleapis.com/maps/api/geocode/json?address='+encodeURI(place)&key=key;
Parse.Cloud.httpRequest({method: 'GET', url: url_gg}).then(function(response){
console.log(response.text);
console.log(response.data);
});
Here is the log from the response.text
"results" : [
{
"address_components" : [
{
... (truncated)
Here is the log from the response.data
{"results":[],"status":"OK"}
When I try to JSON.parse(response.text), I get the same result: the "results" array is empty while it is not empty in the response.text
How can I get the value of the results array?
Thanks a lot!
Maxime.
EDIT: Here is the reponse
{"uuid":"b6efde3a-759b-1fa3-46e5...","status":200,"headers":{"Access-Control-Allow-Origin":"*","Alt-Svc":"quic=\":443\"; p=\"1\"; ma=604800","Alternate-Protocol":"443:quic,p=1","Cache-Control":"public, max-age=86400","Content-Encoding":"gzip","Content-Type":"application/json; charset=UTF-8","Date":"Fri, 04 Sep 2015 18:10:55 GMT","Expires":"Sat, 05 Sep 2015 18:10:55 GMT","Server":"mafe","Transfer-Encoding":"chunked","Vary":"Accept-Language","X-Frame-Options":"SAMEORIGIN","X-XSS-Protection":"1; mode=block"},"text":"{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"1\",\n \"short_name\" : \"1\",\n \"types\" : [ \"street_number\" ]\n },\n {\n \"long_name\" : \"Chemin de ...\",\n \"short_name\" : \"Chemin de ...\",\n \"types\" : [ \"route\" ]\n },\n {\n \"long_na... (truncated)
enter code here

Related

Access a JSON string value, undefined

I have this example where I am trying to access a JSON value, but I get undefined value for id.
My JSON:
{
"success": true,
"msg": "",
"obj": [
{
"remark": "test-1",
"settings": "{\n \"clients\": [\n {\n \"id\": \"430c867306d8\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
},
{
"remark": "test-2",
"settings": "{\n \"clients\": [\n {\n \"id\": \"9831d43186de\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
}
]
}
I want to fetch remark and id, so I wrote:
const remark = data.obj[i].remark;
const settings = data.obj[i].settings.clients[0].id;
Note: data is where my data is in actual code.
settings should be parsed before using
const settings = JSON.parse(data.obj[i].settings).clients[0].id;

Nativescript/Google Geocoding JSON Parse

I am reading a geocoding from Google API in my Nativescript code. The results are coming this way:
{"_bodyInit":"{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"Cooper City\",\n \"short_name\" : \"Cooper City\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"Broward County\",\n \"short_name\" : \"Broward County\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"Florida\",\n \"short_name\" : \"FL\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n }\n ],\n \"formatted_address\" : \"Cooper City, FL, USA\",\n \"geometry\" : {\n
when reading from this code:
fetchModule.fetch(geoPlace, {
method: "GET"
})
.then(function(response) {
alert({title: "GET Response", message: JSON.stringify(response), okButtonText: "Close"});
console.log(JSON.stringify(response))
}, function(error) {
console.log(JSON.stringify(error));
})
My question is, how to get access, for example, to "Cooper City"?
I have tried (without success):
console.log(response.value["results"])
console.log(response[0])
console.log(response.results[0])
making a revision of the response I identify that it is not a successful JSON format, you should look for the way to convert it into JSON format to be able to use its code and get to the tag you need.
https://jsonformatter.curiousconcept.com/
Here's how I found the solution:
var m = JSON.parse(response._bodyInit)
console.log(m.results[0].address_components[1].short_name)
For some reason I don't know the original results did not return as a valid JSON.

How to convert dictionary to json in Swift, and delete "optional" in json string

I want to convert a dictionary to json, dictionary as following:
[
[
"user_event" : "Optional({\n \"strokes\" : {\n \"oldText\" : \"er\",\n \"selection\" : \"error\",\n \"candidates\" : \"error|errors|era\",\n \"context\" : \"error \"\n }\n})",
"device_type" : "x86_64",
"language" : "EN_US"
],
[
"user_event" : "Optional({\n \"strokes\" : {\n \"oldText\" : \"\",\n \"selection\" : \"messages\",\n \"candidates\" : \"messages|message|in\",\n \"context\" : \"error messages \"\n }\n})",
"device_type" : "x86_64",
"language" : "EN_US"
]
]
I'm using following code:
let data = try NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions())
let json = NSString.init(data: data, encoding: NSUTF8StringEncoding)
But the results string has characters as "Optional(" which will cause the json decoder process fail, how can I exclude these additional characters?

How to do nested boolean filters for finding exact matcing values on same field in elasticsearch?

My Index mapping is shown below:
curl -X PUT localhost:9200/testing/listings/_mapping -d '{
"listings" : {
"properties" : {
"address" : {
"properties": {
"location": { "type" : "string",
"index" : "not_analyzed"
}
}
},
"suggest" : {
"type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}'
I need to perform the action of following query in elasticsearch
SELECT * FROM LISTINGS WHERE (published = true AND inActive = false) AND (residentialKind = "Villa" OR residentialKind = "Apartment");
To execute above query action i used the following nested bool query in elasticsearch.
{"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
}
}
Its giving following error
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[B5SbtdlkQTGL0WU-dvg2yg][propgod][0]: SearchParseException[[propgod][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][1]: SearchParseException[[propgod][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][2]: SearchParseException[[propgod][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][3]: SearchParseException[[propgod][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][4]: SearchParseException[[propgod][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }]",
"status": 400
}
So if i wrong with my query please help me to fix this query. Thanks in Advance.
"match" is a query, not a filter, so you're getting an error because you tried to use it as a filter. This should do what you want (assuming "residentialKind" isn't analyzed):
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
If "residentialKind" IS analyzed with something like the standard analyzer, this should work:
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"match":{"residentialKind":"Villa"}},
{"match":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
or even
POST /test_index/_search
{
"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"term":{"published":true}},
{"term":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"villa"}},
{"term":{"residentialKind":"apartment"}}
]
}
}
]
}
}
}
}
}
Here is the code I used for testing:
http://sense.qbox.io/gist/bbed61ef297b058c92d9c7f1523479dfeb3c35b2

jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema level: "fatal"

My schema is :
{
"title": "Order",
"description": "An order from oms",
"type": "object",
"properties": {
"order_id": {
"description": "The unique identifier for an order",
"type": "number"
},
"order_bill_from_party_id": {
"description": "The unique identifier for a party",
"type": "string"
},
"test":{
"type":"integer"
}
},
"required": ["order_id"]
}
My input is :
{"order_bill_from_party_id":"abc",
"order_id":1234}
My validator code :
val factory : JsonSchemaFactory= JsonSchemaFactory.byDefault()
val validator: JsonValidator = factory.getValidator
val schemaJson: JsonNode = JsonNodeFactory.instance.textNode(schema)
val inputJson = JsonNodeFactory.instance.textNode(input)
println(schemaJson)
val report: ProcessingReport = validator.validate(schemaJson, inputJson)
EDIT :
The schemaJson takes form :
{\n
\"title\": \"Order\",\n
\"description\": \"An order from oms\",\n
\"type\": \"object\",\n
\"properties\": {\n
\"order_id\": {\n
\"description\": \"The unique identifier for an order\",\n
\"type\": \"number\"\n
},\n
\"order_bill_from_party_id\": {\n
\"description\": \"The unique identifier for a party\",\n
\"type\": \"string\"\n
},\n
\"test\":{\n
\"type\":\"integer\"\n
}\n
},\n
\"required\": [\"order_id\"]\n
}
However I am getting an exception :
org.specs.runner.SpecError: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"
org.specs.runner.UserError: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:86)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:103)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:123)
at com.flipkart.marketing.bro.core.ValidationSchema.validate(ValidationSchema.scala:25)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply$mcV$sp(ValidationSchemaTest.scala:29)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply(ValidationSchemaTest.scala:27)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply(ValidationSchemaTest.scala:27)
at org.specs.specification.LifeCycle$class.withCurrent(ExampleLifeCycle.scala:66)
at org.specs.specification.Examples.withCurrent(Examples.scala:52)
However on testing on :
http://json-schema-validator.herokuapp.com/
I am getting a
`success
[]`.
Can someone help me out? Testing sites imply my schema is correct but my code is giving excpetions. I think it is because of the escape characters. How can I fix this and remove the escape characters in the JsonNode?
Thanks in advance!
Use JsonLoader.fromString instead of JsonNodeFactory.instance.textNode to load your schema and your instance:
val schemaJson= JsonLoader.fromString(schema)
val inputJson = JsonLoader.fromString(input)
textNode constructs a single node containing a text value, which is not what you are after.