Please help me in writing the jolt spec to get the expected output with special character "#" as shown below. When I place "#FirstName":"Names.FirstName" in the spec it is not working. Please help in resolving this.
JSON Input:
{
"FirstName": "First"
}
Jolt Spec:
[
{
"operation": "shift",
"spec": {
"FirstName": "Names.FirstName"
}
}
]
Expected Output:
{
"Names" : {
"#FirstName" : "First"
}
}
Updated : As of Jolt version 0.0.21, the "shift" transform now allows special characters to escaped.
This Spec will allow "#FirstName" to be a key in the output.
[
{
"operation": "shift",
"spec": {
"FirstName": "Names.\\#FirstName"
}
}
]
Related
Input:
{
"Remarks":"COMMENTS:(87) Test Comments"
}
Expecting Output is below:
{
"Remarks" : "C(87): TestComments"
"Id" : 87
}
I want to replace COMMENTS:(87) string with C(87): and need to get 87 in brackets and print the same in separate attribute "Id".
Can anyone help on this ?
You can use split function along with modify transformation in order to split the string by opening and closing parentheses, and then combine the desired substrings by using concat function. At the last step get rid of the auxiliary elements through use of remove transformation spec such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"str0": "=split('\\(', #(1,Remarks))",
"str1": "=split('\\)', #(1,str0))",
"str2": "#(1,str1[1])",
"Remarks": "=concat('C(',#(1,str2[0]),'):',#(1,str2[1]))",
"Id": "#(1,str2[0])"
}
},
{
"operation": "remove",
"spec": {
"str*": ""
}
}
]
I'm using Apache NiFi and I get some JSON as input. I want to create a new JSON, as follows:
{
"data": ORIGINAL_JSON_HERE,
"new_field_1": "field_1_value",
"new_field_2": "field_2_value"
}
Where field1,field2 does not depend on the JSON, thus the values are not relevant.
In the following demo-site (made by the original creator of the Java Jolt library):
http://jolt-demo.appspot.com/#inception
If I do the following spec:
[
{
"operation": "shift",
"spec": {
"*": "&"
}
}
]
However, when I use the same spec in NiFi JOLT processors, I get null as output...
How can I find the correct spec for NiFi?
The problem was that I wanted the output JSON to have a data field, but I didn't specifiy an operation to create such field. The following works:
[
{
"operation": "shift",
"spec": {
"*": "data.&"
}
},
{
"operation": "default",
"spec": {
"data": {},
}
}
]
I'm a newbie on Apache Nifi and have the following Problem: I would like to transform a json file as follows:
From:
{
"Property1": "x1",
"Property2": "Tag_**2ABC**",
"Property3": "x3",
"Property4": "x4"
}
to:
{
"**2ABC**_Property1": "x1",
"**2ABC**_Property3": "x3",
"**2ABC**_Property4": "x4"
},
it means: taking the value from a certain Attribute to update all other attributes.
I could find examples using JoltTransformer-Processor that works well when the update is only adding a string. But not for my case
What I've done so far: I have set each Attribute using evaluateJSONPath processor. But I just tried quite a lot of possibilities to use the update Attribute processor to do it without success. All my possible tests looked like (within UpdateAttribute):
Property1 --> ${'Property2':substring(4,6)}"_"${'Property1'}
Using Jolt:
[
{"operation": "modify-overwrite-beta",
"spec": {
"Property1": "${'Property2':substring(4,6)}_${'Property1'}"
}
}
]
Which point am I missing here? Thanks in advance!
I don't know about Nifi, but here is how you can do it in Jolt.
Spec
[
{
"operation": "shift",
"spec": {
// match Property2
"Property2": {
"Tag_*": { // capture the nasty "**2ABC**" part to reference later
// go back up the tree to the root
"#2": {
// match and ignore Property2
"Property2": null,
//
// match Property* and use it and the captured
// "prefix" to create the output key
// &(2,1) references the Tag_*, and pull off the "**2ABC**" part
"Property*": "&(2,1)_&"
}
}
}
}
}
]
I had a problem where I had json that looked like this--where the "key" in the json is variable:
{
"rpt":{
"tests": [
{"kerryA":[1,2,3,4]},
{"wallB":[1,2]},
{"MaryA":[1,2,3]}
]
}
}
I wanted it to look like this:
{
"rpt": {
"tests": [
{
"id":"kerryA",
"testTaken":[1,2,3,4]
},
{
"id":"wallB",
"testTaken":[1,2]
},
{
"name":"MaryA",
"testTaken":[1,2,3]
}
]
}
}
I looked at JsonSurfer and Jolt but they didn't seem to support the transform I was looking for. I solved it by rewriting the ObjectNode by recursing through the json with Jackson. While this wasn't bad, I feel like the json to json tranasformation that I looked at did not support the variable "key" well... Other than rewriting and recursing like I did, is there a more elegant transformation solution?
You can achieve it using JOLT with shift operation:
The spec would be:
[{
"operation": "shift",
"spec": {
"rpt": {
"tests" : {
"*": {
"*": {
"$": "rpt.tests[&2].id",
"#": "rpt.tests[&2].testTaken"
}
}
}
}
}
}
]
Take into account that Shiftr treats JSON arrays in the input data as Maps with numeric keys. That is why the double "*" and why rpt.tests[&2].id refers to the element of the array.
You can get more information about the shift operation at: https://github.com/bazaarvoice/jolt/blob/master/jolt-core/src/main/java/com/bazaarvoice/jolt/Shiftr.java
An example to process it in Java would be:
Object inputJSON = JsonUtils.classpathToObject("/input.json");
List<Object> chainrSpecJSON = JsonUtils.classpathToList("/spec.json");
Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
Object transformedOutput = chainr.transform(inputJSON);
System.out.println(JsonUtils.toPrettyJsonString(transformedOutput));
Where input.json is a file in the classpath with your entry data and spec.json is a file with the before code.
I try to convert JSON to JSON-LD and was wondering if I could use JSON-LD expansion algorithm for creating my converter. Then I could just specify my schema as a context and run the expansion algorithm for doing the conversion. Problem is that I cannot figure out how to define new value objects in context so that the expansion algorithm would work.
Let's say I have this:
{
"timestamp": "2016-01-08T11:01:38Z"
}
and I want to get this:
{
"prefix:time": {"prefix:start": "2016-01-08T11:01:38Z"}
}
I have tried it using the JSON-LD playground with something like this:
{
"#context": {
"timestamp": {
"#id": "prefix:time",
"#value": {"prefix:start": "#value"}
}
},
"timestamp": "2016-01-08T11:01:38Z"
}
But the expanded result looks like this:
[
{
"prefix:time": [
{
"#value": "2016-01-08T11:01:38Z"
}
]
}
]
Is there any way to use the JSON-LD expansion (or other) algorithm to replace the value with a new JSON object?
This Jolt transform does the transform you described.
[
{
"operation": "shift",
"spec": {
"timestamp": "prefix:time.prefix:start"
}
}
]
You can try it out at http://jolt-demo.appspot.com/