Remove first meta key from json records using shell

Hi All,

I need to get rid of initial meta key from json files with enclosed parenthesis from start and end of the lines which has total 4000 lines.

here is the sample Json records :

{"start": true, "meta": {"name": "xyz", "creation": "2017-07-14T16:20:06.000+02:00"}}

I need to remove first key,value and next key with enclosed parenthesis from each lines, like below to process further.

{"name": "xyz", "creation": "2017-07-14T16:20:06.000+02:00"}

Can any one please guide here, Thanks in advance.

Hi, try something like:

sed 's/{.*\({[^}]*}\).*}/\1/' file

To keep only the inner curly brace levels..

or try..

sed 's/{[^{]*//; s/[^}]*}\([^}]*\)$/\1/' file

To remove only the outer level.

3 Likes

Thank you for quick reply, however this is not working as expected in my case , I have nested loop so it has deleted all together the big chunk of first level data.

Hi Cloud_Ninja, I was still posting options, did you try the second option?

Hi Scrutinizer, thank you again for your quick reply. yes option 'second' worked fine for me. I am going to try with larger file and let you know for any issues. It would be a great help if you can just explain what was your approach and syntax explanation for learner like me .

if sed is not necessary

grep -o '{[^}{]\+}'

If you are doing a lot with JSON files in a shell-scripting environment, could I suggest installing jq ? It is certainly in the Ubuntu repositories and probably most other distribution repositories; and Mac users can install it with brew

$ echo '{"start": true, "meta": {"name": "xyz", "creation": "2017-07-14T16:20:06.000+02:00"}}' | jq .meta
{
  "name": "xyz",
  "creation": "2017-07-14T16:20:06.000+02:00"
}

Or if you prefer your output on a single line:

$ echo '{"start": true, "meta": {"name": "xyz", "creation": "2017-07-14T16:20:06.000+02:00"}}' | jq -c .meta
{"name":"xyz","creation":"2017-07-14T16:20:06.000+02:00"}

I know it's not a standard Linux tool but (in my opinion) jq is too useful to ignore.

Andrew

3 Likes

I'm the same opinion as Andrew(apmd47). jq is the greatest command line tool for JSON-parsing and manipulation I know. It has a ton of possibilites to mangle / extract json data in this very small binary(<30K). It's written in portable c, with no runtime dependencies. See: jq

2 Likes