Created
November 12, 2018 22:40
-
-
Save nhtzr/ee1860ce665553e59f9a99db57b3a109 to your computer and use it in GitHub Desktop.
Approach for multi value retrieva in stream mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"A": [ | |
{ | |
"B": { | |
"C": [ | |
{ | |
"D": { | |
"app": "Utility", | |
"date": "2018-10-01", | |
"applicationNumberText": { | |
"electronicText": "15570075", | |
"value": "15570075" | |
} | |
} | |
} | |
] | |
} | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script expects at least jq 1.5 and the following invocation: | |
# jq --stream -n -f questions_53256388.jq | |
def p: map(if type == "number" then "?[]?" else . end) ; # This "?[]?" is just an abitrary flag. Meaning `Any array index` | |
def p(x): {} | path(x) | p ; # We are using this for writing arbritary path expression values with our flag. See line 10 | |
# `-n` flag will make this script to run once | |
# and have the stream values to come through `input` instead of `.`. | |
# We will generate a new object, and then build it by each stream entry we process: | |
reduce inputs as $entry ( {}; first( # Please consider this `first` function as a fancier if elif else statement. | |
(select($entry | length < 2)), # We only want stream entries in which the parser gives us a value. | |
(select($entry[0] | p == p( # Like in ln16, we will compare the entry path value with our expected value | |
.A[0].B.C[0].D.applicationNumberText.electronicText)) | | |
setpath(path(.text); $entry[1]) # In the case of electronicText, we want to extract it into field `text` of our new obj | |
), | |
(select($entry[0] | p == p( | |
.A[0].B.C[0].D.applicationNumberText.value)) | | |
setpath(path(.value); $entry[1]) # In the case of electronicText, we want to extract it into field `value` of our new obj | |
), | |
. # Carry over. This line means our temporal object will be available in our next iteration. | |
)) | |
# Our extracted obj is now available here. See results.json below. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"text": "15570075", | |
"value": "15570075" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment