INDEX builtin with input












1














I'm trying to build an INDEX from a json file which contains a list of flat objects.



If I use the construct



jq 'INDEX(.a + ":" + .b)' < data.json 


I get what i want. But if I use



jq 'INDEX(input; .a + ":" + .b) data.json


then I get the error



jq: error (at data.json:<last-line>): break


The reason I need to use the second form is that I will then need to JOIN another input to the index of the first, and I don't see how to do that from STDIN.



Any ideas how I can get around this?



(Currently running jq-1.6 on MacOS 10.13.6)



Adding input (data.json):



[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
]


And expected output



{
"a1:b1": {
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
"a2:b2": {
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
"a3:b3": {
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
}









share|improve this question
























  • Please add example input and output to your question.
    – oguzismail
    Nov 13 '18 at 8:46






  • 1




    @oguzismail : examples added
    – tyke
    Nov 13 '18 at 10:30
















1














I'm trying to build an INDEX from a json file which contains a list of flat objects.



If I use the construct



jq 'INDEX(.a + ":" + .b)' < data.json 


I get what i want. But if I use



jq 'INDEX(input; .a + ":" + .b) data.json


then I get the error



jq: error (at data.json:<last-line>): break


The reason I need to use the second form is that I will then need to JOIN another input to the index of the first, and I don't see how to do that from STDIN.



Any ideas how I can get around this?



(Currently running jq-1.6 on MacOS 10.13.6)



Adding input (data.json):



[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
]


And expected output



{
"a1:b1": {
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
"a2:b2": {
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
"a3:b3": {
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
}









share|improve this question
























  • Please add example input and output to your question.
    – oguzismail
    Nov 13 '18 at 8:46






  • 1




    @oguzismail : examples added
    – tyke
    Nov 13 '18 at 10:30














1












1








1







I'm trying to build an INDEX from a json file which contains a list of flat objects.



If I use the construct



jq 'INDEX(.a + ":" + .b)' < data.json 


I get what i want. But if I use



jq 'INDEX(input; .a + ":" + .b) data.json


then I get the error



jq: error (at data.json:<last-line>): break


The reason I need to use the second form is that I will then need to JOIN another input to the index of the first, and I don't see how to do that from STDIN.



Any ideas how I can get around this?



(Currently running jq-1.6 on MacOS 10.13.6)



Adding input (data.json):



[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
]


And expected output



{
"a1:b1": {
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
"a2:b2": {
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
"a3:b3": {
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
}









share|improve this question















I'm trying to build an INDEX from a json file which contains a list of flat objects.



If I use the construct



jq 'INDEX(.a + ":" + .b)' < data.json 


I get what i want. But if I use



jq 'INDEX(input; .a + ":" + .b) data.json


then I get the error



jq: error (at data.json:<last-line>): break


The reason I need to use the second form is that I will then need to JOIN another input to the index of the first, and I don't see how to do that from STDIN.



Any ideas how I can get around this?



(Currently running jq-1.6 on MacOS 10.13.6)



Adding input (data.json):



[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
]


And expected output



{
"a1:b1": {
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1"
},
"a2:b2": {
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2"
},
"a3:b3": {
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3"
}
}






jq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 10:16







tyke

















asked Nov 13 '18 at 8:44









tyketyke

234




234












  • Please add example input and output to your question.
    – oguzismail
    Nov 13 '18 at 8:46






  • 1




    @oguzismail : examples added
    – tyke
    Nov 13 '18 at 10:30


















  • Please add example input and output to your question.
    – oguzismail
    Nov 13 '18 at 8:46






  • 1




    @oguzismail : examples added
    – tyke
    Nov 13 '18 at 10:30
















Please add example input and output to your question.
– oguzismail
Nov 13 '18 at 8:46




Please add example input and output to your question.
– oguzismail
Nov 13 '18 at 8:46




1




1




@oguzismail : examples added
– tyke
Nov 13 '18 at 10:30




@oguzismail : examples added
– tyke
Nov 13 '18 at 10:30












1 Answer
1






active

oldest

votes


















1














The first argument of INDEX/2 should be a stream of the items to be indexed; here, . is exactly what we want:



INDEX(.; .a + ":" + .b) 





share|improve this answer





















  • You're right. That is EXACTLY what I want here. Many thanks.
    – tyke
    Nov 13 '18 at 15:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276988%2findex-builtin-with-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The first argument of INDEX/2 should be a stream of the items to be indexed; here, . is exactly what we want:



INDEX(.; .a + ":" + .b) 





share|improve this answer





















  • You're right. That is EXACTLY what I want here. Many thanks.
    – tyke
    Nov 13 '18 at 15:57
















1














The first argument of INDEX/2 should be a stream of the items to be indexed; here, . is exactly what we want:



INDEX(.; .a + ":" + .b) 





share|improve this answer





















  • You're right. That is EXACTLY what I want here. Many thanks.
    – tyke
    Nov 13 '18 at 15:57














1












1








1






The first argument of INDEX/2 should be a stream of the items to be indexed; here, . is exactly what we want:



INDEX(.; .a + ":" + .b) 





share|improve this answer












The first argument of INDEX/2 should be a stream of the items to be indexed; here, . is exactly what we want:



INDEX(.; .a + ":" + .b) 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:37









peakpeak

30.7k83957




30.7k83957












  • You're right. That is EXACTLY what I want here. Many thanks.
    – tyke
    Nov 13 '18 at 15:57


















  • You're right. That is EXACTLY what I want here. Many thanks.
    – tyke
    Nov 13 '18 at 15:57
















You're right. That is EXACTLY what I want here. Many thanks.
– tyke
Nov 13 '18 at 15:57




You're right. That is EXACTLY what I want here. Many thanks.
– tyke
Nov 13 '18 at 15:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276988%2findex-builtin-with-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine