Giter Club home page Giter Club logo

Comments (3)

andrewkcarter avatar andrewkcarter commented on September 27, 2024

I ended up copying and modifying shiftr to maintain an input stack instead of only the current input through the recursive apply() calls, and changed the AtPath reference to leverage BasePathAndGroupReference so I could optionally specify a path index value which is used walk up the stack. With these changes I can now use "@2": "&1.[]" to accomplish what I needed. This enables a basic filter/sort capability where you can use drill down matching as criteria to determine which elements to shift.

[
    {
        "operation": "shift2",
        "spec": {
            "entities": {
                "*": {
                    "type": {
                        "*": {
                            "@2": "&1.[]"
                        }
                    }
                }
            }
        }
    }
]

from jolt.

milosimpson avatar milosimpson commented on September 27, 2024

Interesting idea. Can please show me what the input and output look like for your new spec?

I think I get it now. The "@2" pulls the entire "{ type:alpha, data:foo }" block, then deposits in to "alpha.[]".

Neat.

from jolt.

andrewkcarter avatar andrewkcarter commented on September 27, 2024

One example is demonstrated in my original question, and is this:

Input

{
   "entities":[
      {
         "type":"alpha",
         "data":"foo"
      },
      {
         "type":"beta",
         "data":"bar"
      },
      {
         "type":"alpha",
         "data":"baz"
      }
   ]
}

Spec

[
    {
        "operation": "shift2",
        "spec": {
            "entities": {
                "*": {
                    "type": {
                        "*": {
                            "@2": "&1.[]"
                        }
                    }
                }
            }
        }
    }
]

Output

{
   "alpha":[
      {
         "type":"alpha",
         "data":"foo"
      },
      {
         "type":"alpha",
         "data":"baz"
      }
   ],
   "beta":[
      {
         "type":"beta",
         "data":"bar"
      }
   ]
}

Another (untested) example might be to filter out all but paperback books:
Input

{
    "books": [
        {
            "title": "foo",
            "availability": [
                "online"
            ]
        },
        {
            "title": "bar",
            "availability": [
                "online",
                "paperback"
            ]
        },
        {
            "title": "baz",
            "availability": [
                "paperback"
            ]
        }
    ]
}

Spec

[
    {
        "operation": "shift2",
        "spec": {
            "books": {
                "*": {
                    "availability": {
                        "*": {
                            "paperback": {
                                "@3": "PaperBooks[]"
                            }
                        }
                    }
                }
            }
        }
    }
]

Output

{
   "PaperBooks":[
      {
         "title":"bar",
         "availability":[
            "online",
            "paperback"
         ]
      },
      {
         "title":"baz",
         "availability":[
            "paperback"
         ]
      }
   ]
}

One behavior that could be better with this example though is if I used { "paperback|online": { "@4": "PaperBooks[] } we end up with four books because the object with both paperback and online is duplicated. This is just an undesirable side effect of how matching is performed in the existing shiftr implementation, but that could probably be addressed.

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.