Comments (10)
So your question is "Create key/value pair from 2 values", and generally you can not.
Aka given{
"id": "1234",
"classification": "aclass"
}
There is no way to get{
"1234" : "aclass"
}
This comment has been made in 2014 but I seem to be able to accomplish this task as follows:
[
{
"operation": "shift",
"spec": {
"classification": "@(1,id)"
}
}
]
Is it my misunderstanding of a question or resolution of this limitation since 2014?
from jolt.
Actually, I changed my mind. It's more consistent to move the values of 'name' to the headers array and put the 'value' values into each nested array with 'rows'. Managed to get this working with the RHS '#' operator.
Still, interested to know if the original goal is possible...
from jolt.
So your question is "Create key/value pair from 2 values", and generally you can not.
Aka given
{
"id": "1234",
"classification": "aclass"
}
There is no way to get
{
"1234" : "aclass"
}
The problem is that as Shiftr is walking the input tree, when it finds a match it does not have a mechanism to go get at it's neighbors values. It just has the key, value, and path to that point in the tree.
With respect to your original goal, the goal as stated is not valid Json.
Goal
["event1", "element1", "Severity" : "Fatal" , "status_url" : "https://statusurl.com"],
Valid
["event1", "element1", { "Severity" : "Fatal" } , { "status_url" : "https://statusurl.com" } ] OR
["event1", "element1", "Fatal", "https://statusurl.com"]
I think this is "close" to what you wanted...
{
"rows": [
[ "event1", "element1", "Severity", "Fatal", "status_url", "https://statusurl.com" ],
[ "event2", "element2", "Severity", "Medium", "status_url", "https://pants.com" ]
]
}
Which I can get with the following spec :
[
{
"operation": "shift",
"spec": {
"eventList": {
"events": {
"*": {
"*": "rows[&1]",
"requestedFields": {
"*": {
"*": "rows[&3]"
}
}
}
}
}
}
}
]
from jolt.
Thanks for the reply. That's interesting - I ended up with this:
[
{
"operation": "shift",
"spec": {
"eventList": {
"events": {
"0": {
"requestedFields": {
"*": {
"name": "data.headers",
"value": "data.rows[#5]"
}
},
"*": {
"$": "data.headers",
"@": "data.rows[#3]"
}
},
"*": {
"requestedFields": {
"*": {
"value": "data.rows[#4]"
}
},
"*": "data.rows[#2]"
}
}
}
}
}
]
My understanding from the JavaDoc was that using '#X' as the RHS array index here takes the index of the current event element being processed X levels up. Using your solution like this gives the same output I think:
[
{
"operation": "shift",
"spec": {
"eventList": {
"events": {
"0": {
"*": {
"$": "data.headers",
"@": "data.rows[&2]"
},
"requestedFields": {
"*": {
"name": "data.headers",
"value": "data.rows[&3]"
}
}
},
"*": {
"*": {
"@": "data.rows[&2]"
},
"requestedFields": {
"*": {
"value": "data.rows[&3]"
}
}
}
}
}
}
}
]
Now I'm not sure I understand the difference between '&X' and '#X' in this case. Could you explain?
from jolt.
Now I'm not sure I understand the difference between '&X' and '#X' in this case. Could you explain?
They are getting their values from different places.
'#X' is a synthetic field, calculated and maintained by the Shiftr code. It gets incremented each time there is a match. As such it is always numeric. It was originally added to satisfy the use case of "I want to turn a JSON map into an Array.
See : https://github.com/bazaarvoice/jolt/blob/master/jolt-core/src/test/resources/json/shiftr/mapToList.json
and : https://github.com/bazaarvoice/jolt/blob/master/jolt-core/src/test/resources/json/shiftr/mapToList2.json
'&X' is a reference to how far up the "tree" / walked path to pull a value from. '&X' can either be a String or Number. If you have a RHS of "data.rows[&3]" and the key 3 three levels up is not not numeric, Shiftr will throw as exception as it tries to convert a string into an int.
The reason both of your examples work is that they are two different ways of answering the same question, which is "Which sub-element of 'events' am I on right now? Where 'events' happens to be an array."
In '#X' lingo you are asking 'events' which sub-element is being processed.
In '&X' lingo you are asking the event's child, which sub-element are you?
In your example (or any case where the object at the 'events' level is a list, and not a map), '#X' = '&X+1'.
Things are a big hokey in your example in that you have some special casing of 'events[0]'. Aka
'"value": "data.rows[#5]"' is actually referencing 'eventList', not 'events', but it works because there is only one match for 'eventList'. Aka, the line should be, '"value": "data.rows[#4]"'
from jolt.
Ah, I see - thanks for the help again and many thanks for sharing Jolt!
from jolt.
Your welcome. May I ask what you are using it for?
from jolt.
We have a number of APIs returning JSON data in different formats. I want to transform (or conform) the data server-side to a standard format before passing it to a graphical frontend for display. Jolt is making this a lot easier :-D
from jolt.
Cool. That is pretty much what it was designed for.
from jolt.
Hi ,
I need spec for below input and i need output become like below.can you pls help me on this .
Input:
{
"Result": {
"requestStatus": 0,
"requestCode": "SUCCESS",
"totalReq": 3
},
"APrequest": [
{
"requestor": [
{
"Key": "name",
"Value": "Raj"
},
{
"Key": "id",
"Value": "1234"
},
{
"Key": "branch",
"Value": "IT"
}
],
"ticketDetails": [
{
"Key": "role",
"Value": "admin"
},
{
"Key": "dept",
"Value": "skit"
},
{
"Key": "subdept",
"Value": "skht-skit"
}
]
}
]
}
expected output:
{
"Result": {
"requestStatus": 0,
"requestCode": "SUCCESS",
"totalReq": 3
},
"APrequest": [
{
"requestor":
{"name": "Raj","id":"1234","branch":"branch"},
"ticketDetails":
{"role":"admin","dept":"sk","subdept":"skt"}
}
]
}
i have tried below spec ,but am not getting what i expected above.Can anyone help on this.
[
{
"operation": "shift",
"spec": {
"Result": { "requestStatus": "requestStatus", "requestCode": "requestCode" },
"APrequest": {
"": {
"requestor": {
"": { "Value": "@(1,Key)" }
},
"ticketDetails": {
"*": { "Value": "@(1,Key)" }
}
}
}
}
}
]
from jolt.
Related Issues (20)
- Split items from nested arrays into individual objects HOT 2
- Convert boolean values to int HOT 2
- Need to add an attribute : "includeEmpResponse": true, to the input JSON structure in an array for a particular element using Jolt's spec HOT 5
- Merging two arrays using one common key HOT 1
- How to define customised Functions HOT 3
- Splitting a string which is inside an array of objects HOT 3
- How to display output in specific order (kind of sort or set fields/objects in specific order) HOT 3
- Define additional value in array based on key HOT 4
- Merge sub-arrays having the same main items HOT 2
- handling null values on different key
- Add Value to every List Entry HOT 2
- inquiry on conditional statements - 2 or more object validation at the same time HOT 2
- remove null from simple type array
- ifElse multiple conditions? HOT 2
- Demo site source code
- Input JSON lost when doing a date concat
- bug on reading timestamp value HOT 1
- Sort wont work on an Array of complex Objects HOT 1
- squashNulls and recursivelySquashNulls need to work on arrays HOT 1
- concat elements of array HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jolt.