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;
Related
I am invoking a swagger json object per spec.js inside SwaggerEditorBundle to read local swagger api docs.
The swagger json code is bad formatting in one line inside Swagger Editor, so I need to click on Editor -> Convert JSON to YAML to enforce pretty formatting in YAML-style.
How to execute the javascript function automatically for Convert JSON to YAML after reading of spec.js to ensure always pretty formatting in YAML-style?
index.html
<script src="./dist/swagger-editor-bundle.js"> </script>
<script src="./dist/swagger-editor-standalone-preset.js"> </script>
<script src="./dist/spec.js"> </script>
<script>
window.onload = function() {
const editor = SwaggerEditorBundle({
spec: spec,
dom_id: '#swagger-editor',
layout: 'StandaloneLayout',
presets: [
SwaggerEditorStandalonePreset
]
})
window.editor = editor
}
</script>
spec.js
var spec = {
"consumes": [
"application/json"
],
"info": {
"title": "Simple API overview",
"version": "v2"
},
"paths": {
"/": {
"get": {
"operationId": "listVersionsv2",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 300 response",
"examples": {
"application/json": "{\n \"versions\": [\n {\n \"status\": \"CURRENT\",\n \"updated\": \"2011-01-21T11:33:21Z\",\n \"id\": \"v2.0\",\n \"links\": [\n {\n \"href\": \"http://127.0.0.1:8774/v2/\",\n \"rel\": \"self\"\n }\n ]\n },\n {\n \"status\": \"EXPERIMENTAL\",\n \"updated\": \"2013-07-23T11:33:21Z\",\n \"id\": \"v3.0\",\n \"links\": [\n {\n \"href\": \"http://127.0.0.1:8774/v3/\",\n \"rel\": \"self\"\n }\n ]\n }\n ]\n}"
}
}
},
"summary": "List API versions"
}
},
},
"swagger": "2.0"
}
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
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.
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
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.