Skip to main content
-00 is for NUL-delimited. To slurp it's -0777 or -0400 and above, -0777 being idiomatic. Fix encoding issue in the perl approach.
Source Link
Stéphane Chazelas
  • 584.4k
  • 96
  • 1.1k
  • 1.7k

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 -000777 -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=$valSTRING=$val perl -MJSON -000777 -e '$h=decode_json'$string = $ENV{STRING}; utf8::decode $string; $h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"string"};=$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, stringSTRING, into the Perl code. This is due to reading the JSON document from file in "slurp" mode with -000777.

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:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

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.

added 11 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

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 test_1.

If you have your value in a variable:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

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:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

added 4 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using jq:

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

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

If you have your value in a variable:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

Using jq:

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

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

If you have your value in a variable:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

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 test_1.

If you have your value in a variable:

$ 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 -00 -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 -00 -e '$h=decode_json(<>); $h->{topics}[0]{topic}=$ENV{"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.

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 -00.

added 157 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 492 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading