Search code examples
jsonjsonpath

how to concat two value in jsonPath


i have a request like that;

{
    "i0": "4299",
    "l2": "1788",
    "c1": "3",
    "l4": "0",
    "t35": "V001*",
    "i4":"0",
    "l3":"0",
    "csubeno":"4299"
}

and i have a json file;

"ELJ": { "JTEXT1": { "value": "", "jsonPath": "$.parameters.request['c1']" }, "JNUM1": { "value": "", "jsonPath": "$.parameters.request['i0']"

i need to concat that two value --> c1 and i0 i tried something like that but it doest work.

"jsonPath": "$.parameters.request[c1,i0]"

Any suggestions?

i need to concatenate two value (c1 and i0)


Solution

  • When you have a JSON file (test.json) like:

    { "testA": "valueA", "testB": "valueB" }
    

    you can do:

    cat test.json | jq '."testA" + ."testB"'
    

    to get the output: "valueAvalueB"

    or you can do:

    cat test.json | jq '"\(.testA)\(.testB)"'
    

    to get the output: "valueAvalueB"

    P.S. This was already answer here: https://stackoverflow.com/a/45527623/724039

    P.P.S: jq is the tool that is available from GitHub here: https://jqlang.github.io/jq/

    With your last edit, where output looks more like XML then JSON, you could do, (to get a JSON output!):

    cat test.json | jq '{ "JTEXT1": (.testA + "," + .testB) }'
    

    or

    cat test.json | jq '{ "JTEXT1": "\(.testA),\(.testB)" }'
    

    see: https://jqplay.org/s/ItQ06QjZDkW