How to find and replace multiple field values using jq?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












In the following json file,



{
"email": "xxx",
"pass": "yyy",
"contact": [

"id": 111,
"name": "AAA"

],
"lname": "YYY",
"name": "AAA",
"group": [

"name": "AAA",
"lname": "YYY",

],


I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?










share|improve this question









New contributor




user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    In the following json file,



    {
    "email": "xxx",
    "pass": "yyy",
    "contact": [

    "id": 111,
    "name": "AAA"

    ],
    "lname": "YYY",
    "name": "AAA",
    "group": [

    "name": "AAA",
    "lname": "YYY",

    ],


    I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?










    share|improve this question









    New contributor




    user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In the following json file,



      {
      "email": "xxx",
      "pass": "yyy",
      "contact": [

      "id": 111,
      "name": "AAA"

      ],
      "lname": "YYY",
      "name": "AAA",
      "group": [

      "name": "AAA",
      "lname": "YYY",

      ],


      I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?










      share|improve this question









      New contributor




      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In the following json file,



      {
      "email": "xxx",
      "pass": "yyy",
      "contact": [

      "id": 111,
      "name": "AAA"

      ],
      "lname": "YYY",
      "name": "AAA",
      "group": [

      "name": "AAA",
      "lname": "YYY",

      ],


      I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?







      sed json jq






      share|improve this question









      New contributor




      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 41 mins ago









      Goro

      10.4k64994




      10.4k64994






      New contributor




      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      user2181698

      82




      82




      New contributor




      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user2181698 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          awk is the best available tool for any text processing:



          awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file

          {
          "email": "xxx",
          "pass": "yyy",
          "contact": [

          "id": 111,
          "name": "XXX"

          ],
          "lname": "YYY",
          "name": "XXX",
          "group": [

          "name": "XXX",
          "lname": "YYY",

          ],


          The field separator -F in awk detects the colon :, then awk can see before (i.e. $1) and after (i.e. $2) the :. After then, awk will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name will be replaced by the string of interest (e.g. XXX).






          share|improve this answer






















          • Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
            – user2181698
            55 mins ago










          • No replacement happened.. It still shows the old value
            – user2181698
            53 mins ago










          • Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
            – Goro
            48 mins ago


















          up vote
          0
          down vote













          Using jq based on the walk function (needs a recent version):



          jq 'walk(.name?="XXX")' file


          If your jq doesn't support the walk function, just define it as:



          jq '
          # Apply f to composite entities recursively, and to atoms
          def walk(f):
          . as $in
          | if type == "object" then
          reduce keys as $key
          ( ; . + walk(f)) ) | f
          elif type == "array" then map( walk(f) ) | f
          else f
          end;
          walk(.name?="XXX")
          ' file


          Credits: https://github.com/stedolan/jq/issues/963






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            user2181698 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476536%2fhow-to-find-and-replace-multiple-field-values-using-jq%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            awk is the best available tool for any text processing:



            awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file

            {
            "email": "xxx",
            "pass": "yyy",
            "contact": [

            "id": 111,
            "name": "XXX"

            ],
            "lname": "YYY",
            "name": "XXX",
            "group": [

            "name": "XXX",
            "lname": "YYY",

            ],


            The field separator -F in awk detects the colon :, then awk can see before (i.e. $1) and after (i.e. $2) the :. After then, awk will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name will be replaced by the string of interest (e.g. XXX).






            share|improve this answer






















            • Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
              – user2181698
              55 mins ago










            • No replacement happened.. It still shows the old value
              – user2181698
              53 mins ago










            • Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
              – Goro
              48 mins ago















            up vote
            4
            down vote













            awk is the best available tool for any text processing:



            awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file

            {
            "email": "xxx",
            "pass": "yyy",
            "contact": [

            "id": 111,
            "name": "XXX"

            ],
            "lname": "YYY",
            "name": "XXX",
            "group": [

            "name": "XXX",
            "lname": "YYY",

            ],


            The field separator -F in awk detects the colon :, then awk can see before (i.e. $1) and after (i.e. $2) the :. After then, awk will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name will be replaced by the string of interest (e.g. XXX).






            share|improve this answer






















            • Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
              – user2181698
              55 mins ago










            • No replacement happened.. It still shows the old value
              – user2181698
              53 mins ago










            • Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
              – Goro
              48 mins ago













            up vote
            4
            down vote










            up vote
            4
            down vote









            awk is the best available tool for any text processing:



            awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file

            {
            "email": "xxx",
            "pass": "yyy",
            "contact": [

            "id": 111,
            "name": "XXX"

            ],
            "lname": "YYY",
            "name": "XXX",
            "group": [

            "name": "XXX",
            "lname": "YYY",

            ],


            The field separator -F in awk detects the colon :, then awk can see before (i.e. $1) and after (i.e. $2) the :. After then, awk will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name will be replaced by the string of interest (e.g. XXX).






            share|improve this answer














            awk is the best available tool for any text processing:



            awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file

            {
            "email": "xxx",
            "pass": "yyy",
            "contact": [

            "id": 111,
            "name": "XXX"

            ],
            "lname": "YYY",
            "name": "XXX",
            "group": [

            "name": "XXX",
            "lname": "YYY",

            ],


            The field separator -F in awk detects the colon :, then awk can see before (i.e. $1) and after (i.e. $2) the :. After then, awk will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name will be replaced by the string of interest (e.g. XXX).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 30 mins ago

























            answered 1 hour ago









            Goro

            10.4k64994




            10.4k64994











            • Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
              – user2181698
              55 mins ago










            • No replacement happened.. It still shows the old value
              – user2181698
              53 mins ago










            • Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
              – Goro
              48 mins ago

















            • Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
              – user2181698
              55 mins ago










            • No replacement happened.. It still shows the old value
              – user2181698
              53 mins ago










            • Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
              – Goro
              48 mins ago
















            Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
            – user2181698
            55 mins ago




            Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
            – user2181698
            55 mins ago












            No replacement happened.. It still shows the old value
            – user2181698
            53 mins ago




            No replacement happened.. It still shows the old value
            – user2181698
            53 mins ago












            Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
            – Goro
            48 mins ago





            Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output please let me know ;-)
            – Goro
            48 mins ago













            up vote
            0
            down vote













            Using jq based on the walk function (needs a recent version):



            jq 'walk(.name?="XXX")' file


            If your jq doesn't support the walk function, just define it as:



            jq '
            # Apply f to composite entities recursively, and to atoms
            def walk(f):
            . as $in
            | if type == "object" then
            reduce keys as $key
            ( ; . + walk(f)) ) | f
            elif type == "array" then map( walk(f) ) | f
            else f
            end;
            walk(.name?="XXX")
            ' file


            Credits: https://github.com/stedolan/jq/issues/963






            share|improve this answer
























              up vote
              0
              down vote













              Using jq based on the walk function (needs a recent version):



              jq 'walk(.name?="XXX")' file


              If your jq doesn't support the walk function, just define it as:



              jq '
              # Apply f to composite entities recursively, and to atoms
              def walk(f):
              . as $in
              | if type == "object" then
              reduce keys as $key
              ( ; . + walk(f)) ) | f
              elif type == "array" then map( walk(f) ) | f
              else f
              end;
              walk(.name?="XXX")
              ' file


              Credits: https://github.com/stedolan/jq/issues/963






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Using jq based on the walk function (needs a recent version):



                jq 'walk(.name?="XXX")' file


                If your jq doesn't support the walk function, just define it as:



                jq '
                # Apply f to composite entities recursively, and to atoms
                def walk(f):
                . as $in
                | if type == "object" then
                reduce keys as $key
                ( ; . + walk(f)) ) | f
                elif type == "array" then map( walk(f) ) | f
                else f
                end;
                walk(.name?="XXX")
                ' file


                Credits: https://github.com/stedolan/jq/issues/963






                share|improve this answer












                Using jq based on the walk function (needs a recent version):



                jq 'walk(.name?="XXX")' file


                If your jq doesn't support the walk function, just define it as:



                jq '
                # Apply f to composite entities recursively, and to atoms
                def walk(f):
                . as $in
                | if type == "object" then
                reduce keys as $key
                ( ; . + walk(f)) ) | f
                elif type == "array" then map( walk(f) ) | f
                else f
                end;
                walk(.name?="XXX")
                ' file


                Credits: https://github.com/stedolan/jq/issues/963







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 54 mins ago









                oliv

                1,386210




                1,386210




















                    user2181698 is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    user2181698 is a new contributor. Be nice, and check out our Code of Conduct.












                    user2181698 is a new contributor. Be nice, and check out our Code of Conduct.











                    user2181698 is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476536%2fhow-to-find-and-replace-multiple-field-values-using-jq%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery