Home > Software engineering >  Why does the processing of |INDEX("__string__") filter fail on calling the executable jq-w
Why does the processing of |INDEX("__string__") filter fail on calling the executable jq-w

Time:11-16

If it's possible, I couldn't find the solution in the Internet.
I have JSON file:

{
    "ReferenceNumber": "d4fd41850323d2f6000000b013016327",
    "TimeoutInSec": 900,
    "RequestToUploadFileList": [
        {
            "BlobName": "8377ed3d-1b05-4c76-b718-6fddd46fd298",
            "FileName": "jpk_vat_100-01.xml.zip.aes",
            "Url": "https://taxdocumentstorage09tst.blob.core.windows.net/d4fd41850323d2f6000000b013016327/8377ed3d-1b05-4c76-b718-6fddd46fd298?sv=2015-07-08&sr=b&si=d4fd41850323d2f6000000b013016327&sig=yFXyJdsPPkbE0iQwVs5ccLEYEU0lxQHldbVyPfPciXw=",
            "Method": "PUT",
            "HeaderList": [
                {
                    "Key": "Content-MD5",
                    "Value": "eXkPLHMM dHB5GCFoeAvsA=="
                },
                {
                    "Key": "x-ms-blob-type",
                    "Value": "BlockBlob"
                }
            ]
        },
        {
            "BlobName": "0a80a089-bc10-41e1-a74d-70fd45f27aa3",
            "FileName": "jpk_vat_100-02.xml.zip.aes",
            "Url": "https://taxdocumentstorage09tst.blob.core.windows.net/d4fd41850323d2f6000000b013016327/0a80a089-bc10-41e1-a74d-70fd45f27aa3?sv=2015-07-08&sr=b&si=d4fd41850323d2f6000000b013016327&sig=Fj+Gjn7hCKIM6hSvMBGWBxSOyV7V/LMM9pnenbaoxks=",
            "Method": "PUT",
            "HeaderList": [
                {
                    "Key": "Content-MD5",
                    "Value": "NZew85QTb16mFLzx9cyKzA=="
                },
                {
                    "Key": "x-ms-blob-type",
                    "Value": "BlockBlob"
                }
            ]
        }
    ]
}

I want to split RequestToUploadFileList[0].Url into two parts: before ?sv= and the remaining string.

I am trying to convert it into other JSON using Windows command line

c:\EJPK\exe>type "c:\TEMP\jpk1012\out\JPK_VAT.xml.sig.resp"| d:\curl\send\bin\jq-win32.exe -r "{ReferenceNumber:.ReferenceNumber}   {URI:(.RequestToUploadFileList[0].Url)}   {URI_ABS:.RequestToUploadFileList[0].Url[0:.RequestToUploadFileList[0].Url|index("?sv=")] }  {URI_SAS:.RequestToUploadFileList[0].Url[.RequestToUploadFileList[0].Url|index("?sv="):] }   {FILE:.RequestToUploadFileList[0].FileName}   {Key:.RequestToUploadFileList[0].HeaderList[0].Value}"

I am using a formula tested on jqplay.org.

Clearly the problem is in the filter concatenation or putting string into INDEX filter:

.RequestToUploadFileList[0].Url|index("?sv=")

The source/original formula build with jqplay.org:

jq '{ReferenceNumber:.ReferenceNumber}
      {URI:(.RequestToUploadFileList[0].Url)}
      {URI_ABS:.RequestToUploadFileList[0].Url[0:.RequestToUploadFileList[0].Url|index("?sv=")] }
      {URI_SAS:.RequestToUploadFileList[0].Url[.RequestToUploadFileList[0].Url|index("?sv="):] }
      {FILE:.RequestToUploadFileList[0].FileName} 
      {Key:.RequestToUploadFileList[0].HeaderList[0].Value}'

CodePudding user response:

You are running into problems because you're not following the quoting rules for the Windows/DOS command line. You would probably find things simpler if you used jq's -f command-line option, which makes all those problems vanish. You're running into these problems because your jq program includes double quotation marks.

Please also note:

  1. jq's filter names are case sensitive, so that INDEX and index are quite different;
  2. If you are going to use index, it would be better to use it just once, perhaps storing it as a jq variable;
  3. Your jq program can be greatly simplified by "lifting" .RequestToUploadFileList[0]:
{ReferenceNumber:.ReferenceNumber}
   (.RequestToUploadFileList[0]
    | (.Url|index("?sv=")) as $ix
    | {URI:.Url,
        URI_ABS: .Url[0:$ix],
        URI_SAS: .Url[$ix:],
        FILE: .FileName,
        Key: .HeaderList[0].Value } )
  1. For robustness, you might want to deal with the case when $ix is null.

  2. Instead of using index, you might wish to consider using capture:

(.Url|capture( "(?<head>.*)(?<tail>\\?sv=.*)" )) as $url ...
  • Related