Im using SwiftyJSON, Alamofire_SwiftyJSON and Alamofire libraries. I'm trying to parse my response but it returns nothing("definitions" fails to print).
JSON isn't empty it has got the response.
So, my code looks like the following (nothing fails, it all compiles)
Alamofire.request(request).responseSwiftyJSON { dataResponse in
if let JSON = dataResponse.result.value {
print(JSON)
if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string {
print(definitions)
print("Hello")
}}
}
My response model looks like (this is not the whole response, that's just what I want to reach :
{
"results" : [
{
"language" : "en",
"id" : "ace",
"type" : "headword",
"lexicalEntries" : [
{
"language" : "en",
"entries" : [
{
"etymologies" : [
"Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
],
"grammaticalFeatures" : [
{
"type" : "Number",
"text" : "Singular"
}
],
"homographNumber" : "000",
"senses" : [
{
"definitions" : [
"a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
I think that my problem is in, should I add any parentheses or any symbols?
if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string
You are not indexing the corresponding arrays in your JSON response, to access the definitions array you can simply use
JSON['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions']
Related
I am totally new to groovy script and would like some help to solve this out. I have a JSON response I want to manipulate and get desired parameters back by avoiding duplication. The Json response does not have indexes like 0,1,2.. that I can iterate through.
Here is the response that I want to work with:
{
"AuthenticateV2" : {
"displayName" : "Verification of authentication",
"description" : "notification ",
"smsTemplate" : "authentication.v2.0_sms",
"emailHeaderTemplate" : "v2.0_header",
"emailBodyTemplate" : "html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE"
},
"supportedPlans" : [
"connectGo"
]
},
"PasswordRecovery" : {
"displayName" : "Verification of password recovery",
"description" : "notification",
"smsTemplate" : "recovery.v1.0_sms",
"emailHeaderTemplate" : "recovery.v1.0_header",
"emailBodyTemplate" : "recovery.v1.0_body_html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE",
"adminInitiated" : false,
"authnId" : "AUTHENTICATION_IDENTIFIER",
"authnType" : "EMAIL",
"user" : {
"displayName" : "USER_DISPLAY_NAME"
}
},
"supportedPlans" : [
"connectGo"
]
},
"PasswordReset" : {
"displayName" : "password reset",
"description" : "notification",
"smsTemplate" : "recovery.v1.0_sms",
"emailHeaderTemplate" : "recovery.v1.0_header",
"emailBodyTemplate" : "html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"user" : {
"displayName" : "USER_DISPLAY_NAME"
}
}
The expected output that I want to have:
{
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE",
"customToken" : "VERIFICATION_CODE",
"adminInitiated" : false,
"authnId" : "AUTHENTICATION_IDENTIFIER",
"authnType" : "EMAIL"
}
I need to retrieve all fields under parameters tag and also want to avoid duplication
You should first get familiar with parsing and producing JSON in Groovy.
Then, assuming the provided response is a valid JSON (it's not - there are 2 closing curlies (}) missing at the end) to get all the parameters keys merged into one JSON we have to convert the JSON string into a Map object first using JsonSlurper:
def validJsonResponse = '<your valid JSON string>'
Map parsedResponse = new JsonSlurper().parseText(validJsonResponse) as Map
Now, when we have a parsedResponse map we can iterate over all the root items in the response and transform them into the desired form (which is all the unique parameters keys) using Map::collectEntries method:
Map uniqueParameters = parsedResponse.collectEntries { it.value['parameters'] }
Finally, we can convert the uniqueParameters result back into a pretty printed JSON string using JsonOuput:
println JsonOutput.prettyPrint(JsonOutput.toJson(uniqueParameters))
After applying all the above we'll get the output
{
"displayName": "USER_DISPLAY_NAME",
"actionTokenURL": "VERIFICATION_LINK",
"customToken": "VERIFICATION_CODE",
"adminInitiated": false,
"authnId": "AUTHENTICATION_IDENTIFIER",
"authnType": "EMAIL",
"user": {
"displayName": "USER_DISPLAY_NAME"
}
}
If you want to get rid of user entry from the final output just remove it from the resulting uniqueParameters map (uniqueParameters.remove('user')) before converting it back to JSON string.
I am generating random data from the following JSON/AVRO schema:
{
"type" : "record",
"namespace" : "test",
"name" : "metro_data",
"fields": [
{
"name" : "PersonID",
"type" : "int"
},
{
"name" : "TripStartStation",
"type" : {
"type" : "enum",
"name" : "StartStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO"]
}
},
{
"name" : "TripEndStation",
"type" : {
"type" : "enum",
"name" : "EndStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO""]
}
}
]
}
The above schema generates this, for example:
[ {
"PersonID" : -1089196095,
"TripStartStation" : "WIEHLE_RESTON_EAST",
"TripEndStation" : "SPRING_HILL"
}
I want to take the PersonID number of the schema, and add it to the Attributes. Eg, the blank in this photo needs to pull the actual PersonID number generated from the flow:
I have tried to use EvaluateJSONPath with the following configuration, and that's how I end up with the empty string set under PersonalID:
Is my next processor UpdateAttribute? Not sure how to pull that content. Thanks!
You are having array of json message(s)(ex: [...]) and You need to split the array of json into individual flowfiles using SplitJson processor with split expression as $.*
Then use EvaluateJsonProcessor to extract PersonID value as a attribute.
Flow:
--> SplitJson --> EvaluateJsonPath--> other processors
For more details refer to this link regards to the same issue.
I have an index/type in ES which has the following type of records:
body "{\"Status\":\"0\",\"Time\":\"2017-10-3 16:39:58.591\"}"
type "xxxx"
source "11.2.21.0"
The body field is a JSON.So I want to search for example the records that have in their JSON body Status:0.
Query should look something like this(it doesn't work):
GET <host>:<port>/index/type/_search
{
"query": {
"match" : {
"body" : "Status:0"
}
}
}
Any ideas?
You have to change the analyser settings of your index.
For the JSON pattern you presented you will need to have a char_filter and a tokenizer which remove the JSON elements and then tokenize according to your needs.
Your analyser should contain a tokenizer and a char_filter like these ones here:
{
"tokenizer" : {
"type": "pattern",
"pattern": ","
},
"char_filter" : [ {
"type" : "mapping",
"mappings" : [ "{ => ", "} => ", "\" => " ]
} ],
"text" : [ "{\"Status\":\"0\",\"Time\":\"2017-10-3 16:39:58.591\"}" ]
}
Explanation: the char_filter will remove the characters: { } ". The tokenizer will tokenize by the comma.
These can be tested using the Analyze API. If you execute the above JSON against this API you will get these tokens:
{
"tokens" : [ {
"token" : "Status:0",
"start_offset" : 2,
"end_offset" : 13,
"type" : "word",
"position" : 0
}, {
"token" : "Time:2017-10-3 16:39:58.591",
"start_offset" : 15,
"end_offset" : 46,
"type" : "word",
"position" : 1
} ]
}
The first token ("Status:0") which is retrieved by the Analyze API is the one you were using in your search.
Depending on the HTTP_USER_AGENT I have to return a very specialized formatted version of a json structure to the client.
The json object is generated as usual with standard postgres functions.
Lets assume the generated json looks similar to this:
{
"return_code" : 0,
"payload" : {
"name" : "smith",
"age": 17,
"address" :{
"street" : "<whatever>",
"city" : "<anycity>"
}
}
}
Now under some circumstances I have to return this json in the following format:
{
"return_code" : 0,
"payload" : "{"name" : "smith", "age": 17, "address" :"{"street" : "<whatever>", "city" : "<anycity>"}"}"
}
As you can see the nested payload object should be returned as a string - masking ignored here for better readability.
Further else the address property should also be returned as a string, not as a json object.
My postgres code that should do this is simply:
response := jsonb_set(response, '{payload}', to_jsonb((response->'payload')::text));
But the result from the code above looks like:
{
"return_code" : 0,
"payload" : "{"name" : "smith", "age": 17, "address" :{"street" : "<whatever>", "city" : "<anycity>"}}"
}
Consider the quotes are missing (just two) for the address-object.
How can I fix this?
Thank You!
It seems to me that you need a further level of escaping / quoting on the address property, because you have:
Your substitution: { "payload" : { ... } } -> { "payload" : "{ ... }" }
Extra substitution: { "address": { ... } } -> { "address": "{ ... }" }
You'll need to do this before the existing line, so I think what you want is this (requires an extra jsonb variable, payload):
payload := jsonb_set(response->'payload', '{address}', to_jsonb((response->'payload'->address)::text));
response := jsonb_set(response, '{payload}', to_jsonb(payload::text));
I'm trying to deserialize a JSON object that is held in an Array. I'm using SwiftyJSON but the program is not behaving as I expected.
This is the array:
var GamesList = [JSON]();
The array holds my 2 JSON objects:
{
"game_type" : "TRADITIONAL",
"game_player_winner" : "",
"game_state" : "STARTED",
"self_left" : "2",
"self_right" : "1",
"self_name" : "test2",
"opponent_right" : "1",
"opponent_name" : "test1",
"game_guid" : "153fac87-bfc4-367f-41fa-944753dc32c8",
"game_idle_time" : 755858,
"opponent_left" : "3"
}, {
"game_type" : "TRADITIONAL",
"game_player_winner" : "",
"game_state" : "STARTED",
"self_left" : "2",
"self_right" : "1",
"self_name" : "test2",
"opponent_right" : "1",
"opponent_name" : "johannesswart",
"game_guid" : "153fac87-bfc4-367f-41fa-944753dc32c9",
"game_idle_time" : 755858,
"opponent_left" : "3"
}]hier is je gamestate: Optional({
"game_type" : "TRADITIONAL",
"game_player_winner" : "",
"game_state" : "STARTED",
"self_left" : "2",
"self_right" : "1",
"self_name" : "test2",
"opponent_right" : "1",
"opponent_name" : "testuser",
"game_guid" : "153fac87-bfc4-367f-41fa-944753dc32c9",
"game_idle_time" : 755858,
"opponent_left" : "3"
}
I create a new JSON object and fill it with a value of object 1 from the Array:
var gameState : JSON?;
self.gameState = GamesList[1];
When I print the entire self.gameState object to the console all is well and it looks like what I expected. However when I want to just use 1 value of this JSON object I can't seem to get it to work.
I tried with:
self.gameState["game_type"].string;
And:
var foo = JSON(self.gameState);
But this is both not compiling. What am I doing wrong?
I can see where this can be frustrating, but this is actually a pretty simple fix.
I had your same problem a while back when doing work with AlamoFire.
I can see why it's not compiling though, you are trying to retrieve a key out of an Array like:
self.gameState["game_type"]
but the Array type doesn't have key:value pairs, simple it is accessed via indexes:
self.gameState[1]
Step 1
So instead of an array, get your JSON into a dictionary. After that, follow these steps:
Make sure that your JSON data is being stored in a dictionary, like:
let jsonData: [String:String] = //your JSON
(use the var keyboard if you will be manipulating data in jsonData.
Step 2
After your JSON data is stored in a dictionary, simply do:
let gameState = jsonData["game_type"]!
You should not have a problem after that.
I hope I was able to shed some light, please comment if anything was unclear.