How to base64 decode value of dynamic json array in NiFi? - json

I want to base64 decode encoding string of json array property not using hard code!!
This array size is dynamic.
Is there the way of decoding value using JoltTransformJson in NiFi?
Input JSON
{
"A": [
{
"CC" : "Encoded string",
"DD" : "any string"
},
{
"CC" : "Encoded string",
"DD" : "any string"
}
]
"B": "any string"
}
Output JSON
{
"A": [
{
"CC" : "Decoded string",
"DD" : "any string"
},
{
"CC" : "Decoded string",
"DD" : "any string"
}
]
"B": "any string"
}

with NiFi you can use UpdateRecord processor for update a specific record,
this is the Official Guide - NiFi Record Path Guide.
you need to add base64Decode(/A.CC) and this base64Decode(/B) to your processor.
I leave you the link for a guide with examples and screenshots -> Example

Related

How to replace double quotes in a json string in Golang?

I have a problem parsing a string object to JSON with Golang.
there are double quotes in some fields values.
I'm trying to use a regex pattern but it doesn't work for me, I'm doing the conversion from bigquery which is based on the Golang code but it doesn't work for me.
{
"Responses": [],
"SessionParameters(Updated)": {
"counter-no-match-nombre": "($session.params.counter-no-match, 1)",
"message": "Hello."
},
"SystemFunctionResults": {
"SystemFunctionErrors": [
{
"Message": "Wrong type of argument for function "NONE", expected 1th argument to be one of type: ["number"]."
}
]
}
}
here a example
([:[,{]\s*)"(.?)"(?=\s[:,]}])
Test regex
SELECT
REGEXP_REPLACE('{
"obj1": "value is "test" of test, \"obj1\" val",
"result": "your result is "out" in our website",
"info": {
"x": [
{
"desc": "shjjfdsajfjkfsdkf, "sfsdfsdfdsf" fjkdfgjkfd."
}
]
}
}','([:\\[,{]\\s*)"(.*?)"(?:\\s*[:,\\]}])','NONE')
AS json_data;
I would appreciate your help, thanks

how to remove [] character at extracted json using json path

{
"responseCode": "200",
"data": {
"sequence": 1,
"used": true,
"sensingTags": [
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
]
}
}
My goal is get updatedOn value from this json using jsonPath like this
1587557350251
i thought below jsonPath will work but it extract only empty list.
$..sensingTags[?(#.code == 'LED')][0].updatedOn
And i want to know how to extract value like below
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
Not like this one.
[
{
"code" : "LED",
"value" : 1,
"updatedOn" : 1587557350251
}
]
As per Getting a single value from a JSON object using JSONPath, JsonPath will always return an array (or a false) at that point...
Best you can do is process it as an array of updatedOn and simply always grab the first value.
$..sensingTags[?(#.code == 'LED')].updatedOn

My JSON parsing fails

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']

Search inside JSON with Elastic

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.

How to find JsonParse exception in json

I am trying to parse below json. This has to throw me the error saying incorrect json format. But the parsor parses the json only till "value:15" and is not throwing any exception. How can I achieve this?
String json = { and : [{key: domain, value: cricket}, {key : STAT_CDE,value : 15}]}, { and : [{key: domain, value: football}, {key : STAT_CDE,value : 10}]}
Sample Code I am using:
import org.codehaus.jackson.map.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
mapper.readTree(json); //this line is not throwing me any exception
Here is the code snippet:
import org.codehaus.jackson.map.ObjectMapper;
public class JsonTestParse {
public static void main(String[] args) {
String json = "{ \"and\" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \"cricket\"}, {\"key\" : \"STAT_CDE\",\"op\" : \"=\",\"value\" : \"13\"}]},"+
"{ \"and\" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \"Football\"}, {\"key\" : \"STAT_CDE\",\"op\" : \"=\",\"value\" : \"10\"}]}";
System.out.println("json: " + json);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readTree(json);
} catch (Exception e) {
System.out.println("object mapper exp");
}
System.out.println("mapper complete");
}
}
And the output:
line 1: json: { "and" : [{"key": "domain", "op": "=", "value": "cricket"}, {"key" : "STAT_CDE","op" : "=","value" : "13"}]},{ "and" : [{"key": "domain", "op": "=", "value": "Football"}, {"key" : "STAT_CDE","op" : "=","value" : "10"}]}
line 2: mapper complete
The issue is that there is no problem with that json format! Syntactically, you're assigning a comma-delimited list to json, which is perfectly valid code - it will set json to the first item in the list and not do anything with the rest of the values.
After your code executes, json looks like this:
String json = { and : [{key: domain, value: cricket}, {key : STAT_CDE,value : 15}]}
And this value has been ignored entirely:
{ and : [{key: domain, value: football}, {key : STAT_CDE,value : 10}]}
As you can see, json is structured perfectly well.
Also, it looks like you're expecting a String object, but you're supplying a map instead.
Try the following:
String json = "{ and : [{key: domain, value: cricket}, {key : STAT_CDE,value : 15}]}, { and : [{key: domain, value: football}, {key : STAT_CDE,value : 10}]}"
And then parse json. This will most certainly fail because the keys are not enclosed with double quotes (the " character), which is a requirement of json format.
EDIT
To keep up to date with the question:
Here is a formatted view of the json you say you're working with:
{
"and" : [
{"key": "domain", "op": "=", "value": "cricket"},
{"key" : "STAT_CDE","op" : "=","value" : "15"}
]
},
{
"and" : [
{"key": "domain", "op": "=", "value": "football"},
{"key" : "STAT_CDE","op" : "=","value" : "10"}
]
}
The issue is that this is not a json object - it is TWO SEPARATE json objects, each of which is well-formed. I am guessing that ObjectMapper parses a full json structure, and ignores any trailing data without throwing an error.
If you'd like to capture the entire structure in json you will need to enclose them together, likely using an array:
[
{
"and" : [
{"key": "domain", "op": "=", "value": "cricket"},
{"key" : "STAT_CDE","op" : "=","value" : "15"}
]
},
{
"and" : [
{"key": "domain", "op": "=", "value": "football"},
{"key" : "STAT_CDE","op" : "=","value" : "10"}
]
}
]