Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional value to -e #36

Open
sfussenegger opened this issue Sep 26, 2014 · 8 comments
Open

Add an optional value to -e #36

sfussenegger opened this issue Sep 26, 2014 · 8 comments

Comments

@sfussenegger
Copy link

This is the JSON snippet I'm working on (as returned by aws cli):

{
  "Outputs": [
   {
    "OutputValue": "value1",
    "OutputKey": "key1"
   },
   {
    "OutputValue": "value2",
    "OutputKey": "key2"
   }
  ]
}

I want jshon to get the OutputValue where the OutputKey is 'key2'. 'value2' that is. Currently, I don't see a way to do this, so I'd like to suggest an optional value to the -e parameter:

jshon -e Outputs -a -e OutputKey=key2 -p -e OutputValue -u

@vivien
Copy link

vivien commented Sep 26, 2014

Is the index reliable jshon -e Outputs -e 1 -e OutputValue -u?

@sfussenegger
Copy link
Author

That's what I've used as a workaround for now. It looks like, but since it's not under my control, I can't really rely on it.

@vivien
Copy link

vivien commented Sep 26, 2014

True. The problem with your suggestion is that "key=val" is a valid JSON string.

{
 "Outputs": [
  {
   "OutputValue": "value1",
   "OutputKey=key2": "key1"
  },
  {
   "OutputValue": "value2",
   "OutputKey": "key2"
  }
 ]
}

And jshon -e Outputs -e 0 -e OutputKey=key2 gives you "key1".

@sfussenegger
Copy link
Author

Yeah, i thought about that, but wasn't sure if it is. If you start escaping things, it gets tricky, not to mention backward compatibility. So maybe precede -e with -s (just like inserting)?

jshon -e Outputs -a -s key2 -e OutputKey -p -e OutputValue -u

Or add an extra flag

jshon -e Outputs -a -e OutputKey -v key2 -p -e OutputValue -u

@keenerd
Copy link
Owner

keenerd commented Oct 3, 2014

Jshon is not really meant to be used that way. It is for safely extracting segments of json information. It can't do anything smart with the data. It passes off the heavy lifting to the standard text-processing tool chain. Generally the form would look something like

.... | jshon -e Outputs -a -e OutputValue -j -p -e OutputKey -j | \
paste -s -d '\t\n' | grep '^"value2"' | cut -f 2

Or a trivial bit of Awk. Since this is a very small and contrived example I can't give you good advice.

@sfussenegger
Copy link
Author

Well, I don't see how the suggested features isn't about "safely extracting segments of json". All I suggest is filtering a list of nodes by value instead of key. Admittedly, this might be considered a corner case, but a not too complicated one at that.

Honestly though, I believe that everytime somebody uses awk for JSON or XML processing ... God kills a kitten. Seriously. :)

@vivien
Copy link

vivien commented Oct 24, 2014

I prefer the -v approach to extract by value, but using -a is wrong, since a map should return the same amount of object. I'm thinking about a select operator:
-w [<key>] [-v <value>] returning all objects matching key and/or value (no extracting, so no need to rewind).

Given @sfussenegger's example, these calls would be similar:

jshon -e Outputs -a -e OutputValue
jshon -e Outputs -w OutputKey -e OutputValue

and:

jshon -e Outputs -e 1 -e OutputValue
jshon -e Outputs -w OutputKey -v key2 -e OutputValue

So -w would act as a conditional -a ... -p.
It might also be useful to match only by value, with -wv value, didn't think that much about that.

@Napsty
Copy link

Napsty commented Sep 24, 2020

Good idea. I'd love to see this select (kind of conditional) parameter -w in the future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants