Remove characters after last occurrence of the character of a JSON key value












0















I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.



I tried to use regex and replace to remove the strings. I am new to JSON and programming.



- name: Get the Hello-world arn
set_fact:
populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"

- debug:
var: populate_arn


Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World



Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50










share|improve this question



























    0















    I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.



    I tried to use regex and replace to remove the strings. I am new to JSON and programming.



    - name: Get the Hello-world arn
    set_fact:
    populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"

    - debug:
    var: populate_arn


    Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World



    Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50










    share|improve this question

























      0












      0








      0








      I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.



      I tried to use regex and replace to remove the strings. I am new to JSON and programming.



      - name: Get the Hello-world arn
      set_fact:
      populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"

      - debug:
      var: populate_arn


      Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World



      Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50










      share|improve this question














      I am trying to automate the deployment and invoke lambda function using ansible playbook.To get the arn of the lambda function,I wrote a json query. But here, I need only arn(arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World) but the Ansible script generates arn with version number (arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50). I used set_fact and json query to get the arn. But, I want to remove the version number and colon at the end.



      I tried to use regex and replace to remove the strings. I am new to JSON and programming.



      - name: Get the Hello-world arn
      set_fact:
      populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn')}}"

      - debug:
      var: populate_arn


      Expected result:arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World



      Actual result: arn:aws:lambda:us-west-2:1234567890:function:dev-Hello-World:50







      ansible yaml json-query






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 22:26









      BharatBharat

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50 qualifier, you will get whatever is the :$LATEST which can absolutely produce erroneous results



          That said, the output from json_query is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace to strip the qualifier:



          - set_fact:
          populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"





          share|improve this answer
























          • The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

            – Bharat
            Jan 4 at 19:13











          • Thanks Matthew, can you explain me the chars you used in the regex?

            – Bharat
            Jan 4 at 19:56











          • the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

            – Matthew L Daniel
            Jan 4 at 20:29











          • Thanks Matthew for the detailed explanation.

            – Bharat
            Jan 7 at 21:13











          • Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

            – Bharat
            Jan 11 at 2:04












          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%2f54013972%2fremove-characters-after-last-occurrence-of-the-character-of-a-json-key-value%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









          0














          Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50 qualifier, you will get whatever is the :$LATEST which can absolutely produce erroneous results



          That said, the output from json_query is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace to strip the qualifier:



          - set_fact:
          populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"





          share|improve this answer
























          • The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

            – Bharat
            Jan 4 at 19:13











          • Thanks Matthew, can you explain me the chars you used in the regex?

            – Bharat
            Jan 4 at 19:56











          • the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

            – Matthew L Daniel
            Jan 4 at 20:29











          • Thanks Matthew for the detailed explanation.

            – Bharat
            Jan 7 at 21:13











          • Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

            – Bharat
            Jan 11 at 2:04
















          0














          Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50 qualifier, you will get whatever is the :$LATEST which can absolutely produce erroneous results



          That said, the output from json_query is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace to strip the qualifier:



          - set_fact:
          populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"





          share|improve this answer
























          • The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

            – Bharat
            Jan 4 at 19:13











          • Thanks Matthew, can you explain me the chars you used in the regex?

            – Bharat
            Jan 4 at 19:56











          • the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

            – Matthew L Daniel
            Jan 4 at 20:29











          • Thanks Matthew for the detailed explanation.

            – Bharat
            Jan 7 at 21:13











          • Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

            – Bharat
            Jan 11 at 2:04














          0












          0








          0







          Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50 qualifier, you will get whatever is the :$LATEST which can absolutely produce erroneous results



          That said, the output from json_query is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace to strip the qualifier:



          - set_fact:
          populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"





          share|improve this answer













          Foremost, as one can see from the Lambda API documentation, that ARN is the correct, stable, identifier for the function. If you remove the :50 qualifier, you will get whatever is the :$LATEST which can absolutely produce erroneous results



          That said, the output from json_query is just a string, so you can continue your jinja2 pipeline with (as you mentioned) regex_replace to strip the qualifier:



          - set_fact:
          populate_arn: "{{ Hello-World | json_query('results[0].configuration.function_arn') | regex_replace(':[^:]+$', '') }}"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 23:34









          Matthew L DanielMatthew L Daniel

          9,48612729




          9,48612729













          • The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

            – Bharat
            Jan 4 at 19:13











          • Thanks Matthew, can you explain me the chars you used in the regex?

            – Bharat
            Jan 4 at 19:56











          • the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

            – Matthew L Daniel
            Jan 4 at 20:29











          • Thanks Matthew for the detailed explanation.

            – Bharat
            Jan 7 at 21:13











          • Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

            – Bharat
            Jan 11 at 2:04



















          • The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

            – Bharat
            Jan 4 at 19:13











          • Thanks Matthew, can you explain me the chars you used in the regex?

            – Bharat
            Jan 4 at 19:56











          • the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

            – Matthew L Daniel
            Jan 4 at 20:29











          • Thanks Matthew for the detailed explanation.

            – Bharat
            Jan 7 at 21:13











          • Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

            – Bharat
            Jan 11 at 2:04

















          The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

          – Bharat
          Jan 4 at 19:13





          The only reason i am removing the version is, I am using this arn to attach it to a cloudwatch event. When i do that the cloudwatch event gets attached the versioned arn instead of latest. So when ever i run a new version of my lambda code, i see multiple entries of lambda targets in the cloudwatch event.

          – Bharat
          Jan 4 at 19:13













          Thanks Matthew, can you explain me the chars you used in the regex?

          – Bharat
          Jan 4 at 19:56





          Thanks Matthew, can you explain me the chars you used in the regex?

          – Bharat
          Jan 4 at 19:56













          the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

          – Matthew L Daniel
          Jan 4 at 20:29





          the : is the literal character, the [^] syntax means "the set of all characters not contained within the brackets", and the + means 1 or more repeats of the characters before it. The $ "anchors" at the end of the string, to keep it from eating any :[^:]+ found within the arn, which would be a ton of them. A little searching for "regex syntax" would have gone a long way, you know

          – Matthew L Daniel
          Jan 4 at 20:29













          Thanks Matthew for the detailed explanation.

          – Bharat
          Jan 7 at 21:13





          Thanks Matthew for the detailed explanation.

          – Bharat
          Jan 7 at 21:13













          Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

          – Bharat
          Jan 11 at 2:04





          Hey Matthew i tried the above regex which you given, it is removing the dev-hello-world. The function name. I checked online, the regex expression is correct. It removes the version number. Is there any help you can do with this thing when running ansible playbook? The above jquery spits the below output. "populate_arn": "arn:aws:lambda:us-west-2:12345678:function"

          – Bharat
          Jan 11 at 2:04




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013972%2fremove-characters-after-last-occurrence-of-the-character-of-a-json-key-value%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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory