Is it possible to concatenate the values of JSON attributes using JOLT? - json

I am wondering if there is any way to concatenate the values of JSON attributes into one new attribute using JOLT transformation.
For example, I have the following JSON:
{
"name": "Mary",
"Year Joined": "2017",
"Gender": "Female"
}
and I would like it to be transformed into this:
{
"new": "Mary_2017_Female"
}
Is it possible to do this using JOLT or are there other alternative ways to do it?

Yes, but it uses a "beta" transform that may change in the future.
http://jolt-demo.appspot.com/#modify-stringConcat

Expanding #Milo answer (not a fan of links outside SO)
Yes, you can achieve this using modify-overwrite-beta transform
The specification for your example would look like:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"new": "=concat(#(1,name),'_',#(1,Year Joined),'_',#(1,Gender))"
}
}
]

Related

how to remove the Brackets in a json file at the end and beginning in Apache Nifi

i have a json file like this one below and try to remove the array's brackets at the beginning and the end
[
{
"a": "1",
"b": "2"
}
]
and i only want the map:
{
"a": "1",
"b": "2"
}
anyone knows which processor to use and how to configure?
Thanks, Lukas
You can use SplitJson Processor.
I've used the following settings for the Processor.
Or you can also use EvaluateJsonPath. Here is my configuration for EvaluateJsonPath.
An alternative way might be adding a JoltTransformJSON processor which will contain this spec
[
{
"operation": "shift",
"spec": {
"*": ""
}
}
]

How do I access certain elements with jmespath using slicing?

I have the following JSON
{
"items": [
{
"configurationStatus": "SYNCED",
"conflictDetectionState": "IN_SYNC",
"connectivityState": "ONLINE",
I can access individual elements inside with items[*].isModel, but, I cannot figure out how to access the first 3 elements. I tried something like this items[*].[0:2], but it didn't work. I am curious how to access the first 3 elements using slicing.
You would possibly face some issue trying to achieve this because, as pointed in the JMESPath documentation, object are:
object (an unordered collection of key value pairs)
Source: https://jmespath.org/specification.html, emphasis, mine
So you might end up with different keys based on the implementation and have really random results.
Now the issue with your approach is that slices can only act on arrays.
A slice expression allows you to select a contiguous subset of an array.
Source: https://jmespath.org/specification.html#slices, emphasis, mine
What you could do then, in order to have an array out of a hash is to use the values function, but mind that you'll lose the key/value association in the process.
Then, given you have an array, you can now apply the slicing technique.
So with the query:
items[].values(#)[0:3]
On the JSON:
{
"items": [
{
"configurationStatus": "SYNCED",
"conflictDetectionState": "IN_SYNC",
"connectivityState": "ONLINE",
"foo": "bar",
"baz": "qux"
},
{
"configurationStatus": "SYNCED′",
"conflictDetectionState": "IN_SYNC′",
"connectivityState": "ONLINE′",
"foo": "bar′",
"baz": "qux′"
}
]
}
This would give:
[
[
"SYNCED",
"IN_SYNC",
"ONLINE"
],
[
"SYNCED′",
"IN_SYNC′",
"ONLINE′"
]
]

How to keep one level of object and extract one of its sub-key only?

I'm playing a bit with terraform state json output and want to transform it a bit.
Given that input issued from terraform:
{
"cost": {
"sensitive": false,
"value": "123"
},
"test_id": {
"sensitive": false,
"value": "6610758455459338306"
}
}
How do i convert it to something usefull for my application like bellow:
{
"cost": "123",
"test_id": "6610758455459338306"
}
I tried to play with from_entries, with_entries but i'm a complete noob at it
You're looking for map_values. It works just like map, but doesn't convert an object input to an array.
map_values(.value)
Online demo
oguz's answer is crisp and to the point, but if you want to do with *_entries functions, you could getaway with
with_entries(.value = .value.value)
jqplay - snippet

Update Json-Attributes in Apache-Nifi: Jolt

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)_&"
}
}
}
}
}
]

Setting value as object in JSON-LD expansion

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/