1

I have a json, from which I need to extract a part and append/insert a new value into the existing array. My code is as follows,

{
    "itemId": "item_1",
    "itemName": "item 1",
    "itemPosition": [{
    "posId": "item_1_1",
    "rowPos": 0,
    "columnPos": 0
}, {
    "posId": "item_1_2",
    "rowPos": 0,
    "columnPos": 1
}, {
    "posId": "item_1_3",
    "rowPos": 1,
    "columnPos": 0
}, {
    "posId": "item_1_4",
    "rowPos": 1,
    "columnPos": 1
}]
}, {
"itemId": "item_2",
"itemName": "item 2",
"itemPosition": [{
    "posId": "item_2_1",
    "rowPos": 0,
    "columnPos": 0
}, {
    "posId": "item_2_2",
    "rowPos": 0,
    "columnPos": 1
}, {
    "posId": "item_2_3",
    "rowPos": 1,
    "columnPos": 0
}, {
    "posId": "item_2_4",
    "rowPos": 1,
    "columnPos": 1
}]
}

in this json i need to add a new value in itemPosition under the item id "item_1" like

{
    "posId": "item_1_5",
    "rowPos": 2,
    "columnPos": 1
}

I used following command to extract the json using string search like

cat sample.json | nl | sed -n '/"item_1"/,/"item_2"/p'

output is :

 2      "itemId": "item_1",
 3      "itemName": "item 1",
 4      "itemPosition": [{
 5          "posId": "item_1_1",
 6          "rowPos": 0,
 7          "columnPos": 0
 8      }, {
 9          "posId": "item_1_2",
10          "rowPos": 0,
11          "columnPos": 1
12      }, {
13          "posId": "item_1_3",
14          "rowPos": 1,
15          "columnPos": 0
16      }, {
17          "posId": "item_1_4",
18          "rowPos": 1,
19          "columnPos": 1
20      }]
21  }, {
22      "itemId": "item_2",

Question is how should I traverse into itemPosition array to find the last value in the array and append or insert a new value after that?

3
  • How would you feel about using a language which actually supports JSON, like Python, Ruby, Perl, etc.? Commented Jan 14, 2015 at 7:25
  • Thanks for your response John. Supposed to use shell scripting only. Commented Jan 14, 2015 at 8:27
  • Your json is broken: cat your.json | python -m json.tool returns Extra data: line 21 column 2 - line 41 column 2 (char 338 - 666) Commented Jan 14, 2015 at 10:24

2 Answers 2

1

you can use the below code, if you dont have jq or any parser installed in your linux box. it should work for you requirement. kindly check.

line_to_be_replaced=`cat itemlist.json | nl |  sed -n '/"item_1"/,/"item_2"/p' | grep -in "}]" | awk '{print $2}'`
sed  -i "${line_to_be_replaced} s|.*|}, {\n\"posId\": \"item_1_5\",\n\"rowPos\": 2,\n\"columnPos\": 1\n}]|g" itemlist.json

Note: itemlist.json file contains the JSON code.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use jq to parse json in the shell. jq makes it possible to selectively append strings to a json file.

This code will achieve what you're trying to do:

cat your.json | jq '.itemPosition |= . + [{"posId":"item_1_5","rowPos": 2,"columnPos": 1}]'

edit: this works only for lines 1 to 21 of your json file which is broken from line 22 on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.