-3

I have a list in JSON file apps.json:

{
"properites":{
   "apps":["apple","orange","mango"]
}
}

I need to read this file and get of objects.

I used:

fwk_app = $( awk '/apps/{ print }' ${apps.json} | grep apps | awk -F: '{ print } substr($0, index($0,$2))}' |sed 's/(,*\)"[/\"]*$/\1/' );
for item in fwk_app:
do
echo"$item"
done

The output is:

mango

The output I need is:

apple
orange
mango
5
  • 4
    Use a proper json parser, such as jq. Everything else is just a recipe for headaches. – Panki Feb 12 at 9:48
  • 1
    jq -r '.properites.apps[]' apps.json (using the misspelling from the example) – Uncle Billy Feb 12 at 9:54
  • @uncle Billy is Jq -r i got same output ["apple","orange","mango"] – Shravya Vaggu Feb 12 at 10:23
  • @ShravyaVaggu You would get that if you forgot to add the [] at the end of the jq expression. You want each individual element of the list, not the list as a whole. – Kusalananda Feb 12 at 10:37
  • i want to out put as: apple orange mango – Shravya Vaggu Feb 12 at 10:46
0

With awk you can get the expected output


awk -v FS=':' -v OFS='\n' '/"apps"/ {gsub(/[]"[]/,"",$2);split($2,a,/,/);print a[1],a[2],a[3]}' file
apple
orange
mango

but I think the correct way is @Kusalananda's solution.

2
  • thanks@Carlos it works . But i have doubt in print a[1],a[2],a[3] it prints only three indexs if add objects in list in future then i need add a[4] . but i don't want add this indexs for add i want direct list can you please help on this – Shravya Vaggu Feb 15 at 7:46
  • If you want an output as: apple orange mango you can test it: awk -v FS=':' '/"apps"/ {gsub(/[]"[]/,"",$2);split($2,a,/,/);print a[1],a[2],a[3]}' file – Carlos Pascual Feb 15 at 13:47
6

The correct way to parse a JSON file is to use a JSON parser. The jq utility is one such parser that is made for use on the command line and in scripts:

jq -r '.properites.apps[]' apps.json

This would output

apple
orange
mango

This asks jq to extract the elements of the array apps in the top-level object properites in the JSON document in the file apps.json. The -r option asks jq to output "raw" data rather than JSON-encoded data.

To get tab-delimited output on a single line, use

jq -r '.properites.apps | @tsv' file

This would output

apple   orange  mango

The @tsv operator takes an array and outputs it as a tab-delimited list. You could instead use @csv to get properly quoted CSV output.

Note that this would also work no matter whether your JSON document was

{
  "properites": {
    "apps": [
      "apple",
      "orange",
      "mango"
    ]
  }
}

or

{"properites":{"apps":["apple","orange","mango"]}}

(these are equivalent forms of the same JSON document).

7
  • tried but i didnot get expected output – Shravya Vaggu Feb 12 at 10:24
  • 2
    @ShravyaVaggu Then, 1) you used another command than what I showed, or 2) you have a JSON document that looks different to what's in your question, or 3) you may be expecting something other than what you say in the question. Consider helping me determine what's wrong by letting me know what happened when you tried. – Kusalananda Feb 12 at 10:34
  • i am using the same command2) i have same JSON document as shown 3) i am expecting to list of objects i have rechecked all the three points you mentioned – Shravya Vaggu Feb 12 at 10:45
  • 1
    so, what output are you getting then? – pLumo Feb 12 at 10:56
  • 3
    Works fine for me. You are not using same command then, might be you are missing the []? – pLumo Feb 12 at 11:03
3

You could use python:

python -c '
import json, sys
data = json.load(sys.stdin)
for el in data["properites"]["apps"]:
  print (el)
' < apps.json
2

is the best answer. ships with a JSON module:

ruby -rjson -e '
    data = JSON.parse(IO.read(ARGV.shift))
    puts data["properites"]["apps"].join("\n")
' apps.json
0

You could also use grep -P if available:

grep -wF  '"apps":' apps.json | grep -Po '[[,]"\K[^"]*'

However, this (and the awk solution provided by @Carlos) works only if the input is exactly as shown, not if the json with same content is formatted differently.

A proper json parser , e.g. jq or python's json module should be preferred!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.