I know there are at least hundreds of examples out there, but I just can't find the one that works best for me.
I have a string that is displayed like this (with all the spaces, the backslashes, etc):
string textInfo = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
i would like to extract for example only the id, or name or message on another string. For example:
string resultID = "109322" //for my id
string resultName = "Name Surname" //for my name
etc...
Thank you very much for your time and effort in helping me.
Kind Regards
What language are you tring to use?
As your string is in JSON format, try parsing it with a JSON parser and then reading from the object
As mentioned by Jon, this string is in JSON (Javascript Object Notation) format.
Each language has a method to convert from JSON into an object from which you can extract each attribute as a property. In javascript you can use JSON.stringify(textInfo) to represent the data in an object form.
The mechanics would look like the following:
jsonString = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
dataObject = JSON.parse(jsonString)
var resultID = dataObject.data.id //for my id
var resultName = dataObject.data.name //for my name
the rest of the properties can be similarly queried.
For reference, here is a "prettified" version of your JSON data:
{
"code":null,
"success":true,
"data":[
{
"id":109322,
"name":"Name Surname",
"display_name":"Name SURNAME",
"city":"Bellairë",
"zip":"24000",
"street":"boulevard 21"
}
],
"message":null,
"total":1
}
I feel so bad not trying harder. Anyway here is my working code for Windows Application C#
string jsonString = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
JObject jsonObj = JObject.Parse(jsonString);
return (string)jsonObj["data"][0]["id"];
And output is: 109322
Related
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;
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.
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
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.
I'm trying to build a query in Elastic search to search inside the text of multiple fields for example:
Title and Description are the fields and I want to search for "obama", "world", "news" inside these 3 fields. I've tryed searching and found information in Elastic website here:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#_literal_fields_literal_and_per_field_boosting
But the problem is when I tried this structures it produce errors:
First:
{
"multi_match" : {
"query": "obama world news",
"type": "cross_fields",
"fields": [ "Title", "Description" ],
"operator": "and"
}
}
It produces error like this:
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }]",
"status": 400
}
I also have tried this query:
{
"sort": {
"re_max": {
"order": "desc"
}
},
"query": {
"multi_match" : {
"query": "obama world news",
"type": "cross_fields",
"fields": [ "Title", "Description" ],
"operator": "and"
}
},
"from": 0,
"size": 30}
}
And this is the result:
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#1ef6a497>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#364d93ed>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#6e98bb14>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#6c0543f7>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#215084be>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }]",
"status": 400
}
I'm using Postman Rest client to run the query. What's the problem with my code?
Try like this
{
"query": {
"multi_match": {
"query": "obama world news",
"type": "cross_fields",
"fields": ["Title", "Description"],
"operator": "and"
}
},
"from": 0,
"size": 20,
"sort": [
{
"re_max": {
"order": "desc"
}
}
]
}
HOpe it helps..!