Git Product home page Git Product logo

Comments (10)

apiwoni avatar apiwoni commented on August 22, 2024 2

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.

rsdb avatar rsdb commented on August 22, 2024

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.

milosimpson avatar milosimpson commented on August 22, 2024

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.

rsdb avatar rsdb commented on August 22, 2024

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.

milosimpson avatar milosimpson commented on August 22, 2024

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.

rsdb avatar rsdb commented on August 22, 2024

Ah, I see - thanks for the help again and many thanks for sharing Jolt!

from jolt.

milosimpson avatar milosimpson commented on August 22, 2024

Your welcome. May I ask what you are using it for?

from jolt.

rsdb avatar rsdb commented on August 22, 2024

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.

milosimpson avatar milosimpson commented on August 22, 2024

Cool. That is pretty much what it was designed for.

from jolt.

Raaz43857 avatar Raaz43857 commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.