Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace an attribute or key in JSON using jq or sed

Have a big json like this

"envConfig": {
    "environmentName": {
        "versions": [
            {
                "name": "version1",
                "value": "Dev"
            },
            {
                "name": "version2",
                "host": "qa"
            }
        ],
        "userRoles": [
            {
                "name": "Roles",
                "entry": [
                    {
                        "name": "employees",
                        "value": "rwx"
                    },
                    {
                        "name": "customers",
                        "value": "rx"
                    }
                ]
            }
        ]
    }
},

I wanted to change the JSON attribute from "environmentName" to "prod". Below is the output i am expecting

"envConfig": {
    "prod": {
        "versions": [
        ...
        ],
        "userRoles": [
        ...
        ]
    }
}

Tried with sed command as below

sed "s/\('environmentName':\)/\1\"prod\"\,/g" version.json

Tried with jq as below but not working

cat version.json | jq  ' with_entries(.value |=   {"prod" : .environmentName} ) '

Any help here to replace the attribute/key of an json with desired value

like image 483
user1876040 Avatar asked Mar 23 '17 06:03

user1876040


People also ask

What is the difference between SED and JQ?

However, there are significant differences between the two applications. Here is an analysis of the similarities and differences between the applications and their relative strengths. jq expects JSON data and is not used with other types of data. sed can process a variety of text-based file formats.

How do I create a JSON object with just two keys?

When creating an object with {}, you specify the names of the keys with unquoted text, and then assign the values with regular jq filters. The resulting set of JSON objects have just two keys: id and title:

What is JQ in Linux?

jq for Creating and Updating JSON jq is an amazing little command line utility for working with JSON data. We’ve written before about how you can use jq to parse JSON on the command line, but in this post I want to talk about using jq to create JSON data from scratch or make changes to existing data.

How easy is JQ to use with JSON?

It is fairly easy to use jq to perform basic operations on JSON files. However, some of the techniques for transforming and reformatting data can be complex. It is often easiest to try jq out on a sample JSON file first. There are plenty of examples on the JSON website that are guaranteed to be correctly formatted.


2 Answers

You weren't too far off with the jq, how about this?

jq '.envConfig |= with_entries(.key |= sub("^environmentName$"; "prod"))'

Two differences: first off, we want to drill down to envConfig before doing a with_entries, and second, when we get there, the thing we want will be a key, not a value. In case there are any other keys besides environmentName they'll be preserved.

like image 71
hobbs Avatar answered Oct 17 '22 22:10

hobbs


TL,TR

You can use the following command:

jq '(.envConfig |= (. + {"prod":.environmentName}|del(.environmentName)))' foo.json

Let's say you have the following json:

{
    "foo": {
        "hello" : "world"
    }   
}

You can rename the node foo to bar by first duplicating it and then remove the original node:

jq '. + {"bar":.foo}|del(.foo)' foo.json

Output:

{
    "bar": {
        "hello" : "world"
    }   
}

It get's a bit more complicated if you want to replace a child key somewhere in the tree. Let's say you have the following json:

{
  "test": {
    "foo": {
      "hello": "world"
    }
  }
}

You can use the following jq command for that:

jq '(.test |= (. + {"bar":.foo}|del(.foo)))' foo.json

Note the additional parentheses and the use of the assignment operator |=.

Output:

{
  "test": {
    "bar": {
      "hello": "world"
    }
  }
}
like image 34
hek2mgl Avatar answered Oct 17 '22 22:10

hek2mgl