1

This is the json file that we want to edit

Example 1

{
  "topics": [{"topic": "hfgt_kejd_tdsfd”}],
  "version": 1
}

Example 2

{
  "topics": [{"topic": "hfgt_kj_fsrfdfd”}],
  "version": 1
}

We want to replace the third word in the line topics with other word ( by sed or perl one liner )

regarding to example 1 , Expected results when we want to replace hfgt_kejd_tdsfd with test_1

{
  "topics": [{"topic": "test_1”}],
  "version": 1
}

example 3

more /tmp/file.json

{
  "topics": [{"topic": "TOPIC_GGGG”}],
  "version": 1
}


# sed  's/\(topic\": "\)[a-z_]*/\1test_1/'  /tmp/file.json

{
  "topics": [{"topic": "test_1TOPIC_GGGG”}],
  "version": 1
}
1
  • 1
    Your closing quote after the topic name is (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) Commented Dec 22, 2019 at 10:14

3 Answers 3

8

Using jq:

$ jq '.topics[0].topic |= "test_1"' file.json
{
  "topics": [
    {
      "topic": "test_1"
    }
  ],
  "version": 1
}

This reads the JSON document and modifies the value of the topic entry of the first element of the array topics to the string test_1.

If you have your value in a variable (encoded in UTF-8):

$ val='Some value with "double quotes"'
$ jq --arg string "$val"  '.topics[0].topic |= $string' file.json
{
  "topics": [
    {
      "topic": "Some value with \"double quotes\""
    }
  ],
  "version": 1
}

Using Perl:

$ perl -MJSON -0777 -e '$h=decode_json(<>); $h->{topics}[0]{topic}="test_1"; print encode_json($h), "\n"' file.json
{"topics":[{"topic":"test_1"}],"version":1}

With a variable:

$ val='Some value with "double quotes"'
$ STRING=$val perl -MJSON -0777 -e '$string = $ENV{STRING}; utf8::decode $string; $h=decode_json(<>); $h->{topics}[0]{topic}=$string; print encode_json($h), "\n"' file.json
{"topics":[{"topic":"Some value with \"double quotes\""}],"version":1}

Both of these uses the Perl JSON module to decode the JSON document, change the value that needs changing, and then output the re-encoded data structure. The error handling is left as an exercise.

For the second piece of code, the value to be inserted is passed as an environment variable, STRING, into the Perl code. This is due to reading the JSON document from file in "slurp" mode with -0777.

9
  • sorry for this note , but the quastion is about to use sed/perl one liner , I prefer to user sed/perl because this is very simple json and no need jq Commented Dec 21, 2019 at 21:47
  • @yael If it simple or not depends on the data that you'd want to insert. With sed, you would need to do the JSON encoding of your string yourself. I will add a Perl variation too (just need to test it first). Commented Dec 21, 2019 at 21:48
  • unfathomably , I not have the module as - Can't locate JSON.pm in @INC (@INC contains: , maybe sed , is better +1 for your effort ) Commented Dec 21, 2019 at 22:11
  • @yael On Ubuntu, it's in the libjson-perl package. Commented Dec 21, 2019 at 22:19
  • I have RHEL - 7.3 Commented Dec 21, 2019 at 22:21
3

Instead of matching [A-Za-z_] (which by the way matches a lot more than the English letters and underscore in most locales), use [^"] for any character other than " (by the way, the sample in your question has a (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) as the closing quote after the topic name, I'll assume that's not the case in your original files as that would make it invalid json):

sed 's/\("topic": "\)[^"]*/\1test_1/' < file.json

(note that it's your responsibility to make sure the topic name replacement is properly JSON-encoded: UTF-8 charset, control characters encoded (like \n for newline), and " encoded as \").

2
sed -i 's/\(topic\": "\)[A-Za-z_]*/\1test_1/' file.json
4
  • I test it , but its just add the test_1 before TOPIC_GGGG ( when topic name - TOPIC_GGGG ) Commented Dec 21, 2019 at 22:14
  • you can see example 3 in my question Commented Dec 21, 2019 at 22:20
  • @yael You could venture to change the regular expression they're showing. It currently expects a text consisting of characters a-z and _ only. Remember to properly escape (or encode) any characters that needs to be escaped in JSON before inserting your new data. Commented Dec 21, 2019 at 22:44
  • As @Kusalananda noted, was trivial to add A-Z. My answer updated so it now meets the recently added test cases in the question. Commented Dec 22, 2019 at 9:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.