-1

I have a json file where i need to change some key:values. The Key and values are dynamic and are stored in a array. I just need to parse the file and replace value corresponding to the particular key.

foo="user"
cat properties.json | jq '.node.${foo}' > tempo.json

So if i tried the following command errors are thrown ..

jq: error: syntax error, unexpected '$', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.master_node.$foo
jq: 1 compile error

Tried lot of commands.. But i could not pass variable while traversing a json tree.. Appreciate any help

Beatrix Kiddo
  • 199
  • 1
  • 2
  • 10
  • How are line ends used? is there one node by line? What characters are used for usernames / passwords? – brunorey Feb 16 '17 at 13:02
  • there are one nodes per line..Username and password can have any character..Dont know how line ends are used..probably new line. – Beatrix Kiddo Feb 16 '17 at 13:15

2 Answers2

0

Use jq, like this:

jq --arg flag "node2" \
   --argjson input '{"user": "jack","password": "killbie"}' \
'(.[$flag]|.user)|=$input.user|(.[$flag]|.password)|=$input.password' your.json 
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Why are you looking for those? Don't use `sed` or `awk` for that. These tools can't reliably solve this. (Even if others might post some hackish answers. Trust me). I recommend to a) install `jq` or b) use a programming language that can deal with json and is installed on any linux distribution, like Python2 – hek2mgl Feb 16 '17 at 13:25
  • @BeatrixKiddo if I were you, I will take hek2mgl's advice. He can certainly post sed/awk solutions, (check his reputation & his profile), but he's chosen `jq` tool, because it is the right way. Same for Xml/html parsing, first thing is, choose the right tool. – Kent Feb 16 '17 at 13:47
  • 1
    @hek2mgl and Kent thanks for your suggestion. I too came to the same conclusion. I just thought there may be some solution available..Anyway thanks again.. – Beatrix Kiddo Feb 16 '17 at 15:21
  • @hek2mgl what to do if "input" in --argjson is not hardcoded and it is in some file or any other variable ? – Beatrix Kiddo Feb 17 '17 at 07:51
  • When it is a another variable, use that variable. When it is in another file, use `--argfile input FILENAME.json` – hek2mgl Feb 17 '17 at 08:18
  • Please edit your question. Don't post code in comments – hek2mgl Feb 17 '17 at 10:29
  • Please take a little moment and read the formatting help to make the code in the answer nicely formatted and indented. That's your part of work, I'll try to answer then – hek2mgl Feb 17 '17 at 11:00
  • 1
    @hek2mgl Thanks a lot for your help. I solved my problem.Your first solution works for me.I was doing some syntax error. – Beatrix Kiddo Feb 18 '17 at 06:00
-1

If you intend to change every user and password, regardless of the node name, you could just use sed:

cat file.json | sed "s/\"user\": \"[0-9a-zA-Z]*\", /\"user\": \"jack\", /" | sed "s/\"password\": \"[0-9a-zA-Z@]*\", /\"password\": \"killbie\", /"

Keep in mind that you need to specify the character group to match ([0-9a-zA-Z@]), in this case I have put characters I saw in your example.

In case you need to replace it only for some nodes, or do some more complicated conditions, then you might need a specific json analyzer tool like jq (outside the scope of "inbuilt unix tools") or some higher level scripting language like python (possibly inline python like this case). Doing it with sed would only work as long as the node name and the attributes are in the same line. I'd do it with backreferences (\1, \3). Here's an example:

sed "s/\(node2.*\)\(\"user\": \"[0-9a-zA-Z]*\", \)\(.*\)}/\1\"user\": \"jack\", \3/"
Community
  • 1
  • 1
brunorey
  • 2,135
  • 1
  • 18
  • 26
  • thanks for the sugesstion . Is there any reg ex to identify the required term ..here "user" which is inbetween "node2" and "}" ? – Beatrix Kiddo Feb 16 '17 at 13:31