0

I have a sample file relative.json with the following entries:

{
    "RELATIVES": [
        {
            "Name": "Ram",
            "Country": "India",
            "Phone": "111111111",           
        },
        {
            "Name": "James",
            "Country": "US",
            "Phone": "111111111",           
        },
        {
            "Name": "Robin",
            "Country": "UK",
            "Phone": "111111111",           
        },
        {
            "Name": "Shyam",
            "Country": "India",
            "Phone": "111111111",           
        },
        {
            "Name": "Jon",
            "Country": "US",
            "Phone": "111111111",           
        }
    ]
}

I am looking to write a simple code to read all these entries, put it into a variable and echo it eg. Relative Name is Ram from India and phone# 111111111 Relative Name is James from India and phone# 111111111 Relative Name is Robin from India and phone# 111111111

I am very new to jquery, but good at the shell. Please help me to write this

4
  • Please add your desired output (no description) for that sample input to your question (no comment). Commented Oct 26, 2020 at 8:40
  • Take a look at jq to parse JSON. Commented Oct 26, 2020 at 8:41
  • parse error: Expected another key-value pair at line 7, column 9 and fix that sample data. Thanks. Commented Oct 26, 2020 at 8:59
  • Check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… document. This page contains example also. Commented Oct 27, 2020 at 10:59

2 Answers 2

1

Your json sample is incorrect and this is why jq is complaining. You should not have any commas at the end of the phone numbers and so the json should actually be:

{
    "RELATIVES": [
        {
            "Name": "Ram",
            "Country": "India",
            "Phone": "111111111"          
        },
        {
            "Name": "James",
            "Country": "US",
            "Phone": "111111111"           
        },
        {
            "Name": "Robin",
            "Country": "UK",
            "Phone": "111111111"          
        },
        {
            "Name": "Shyam",
            "Country": "India",
            "Phone": "111111111"           
        },
        {
            "Name": "Jon",
            "Country": "US",
            "Phone": "111111111"          
        }
    ]
}

With the amended data in a file called relatives, you can use jq to achieve the output required as follows:

cat relatives | jq -r '.RELATIVES[] | "Name is "+.Name+" from "+.Country+" and phone number is "+.Phone'

-r removes the quotations around the output and + concatenates strings with values.

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

Comments

0

I'd use GNU awk and JSON extension for the job:

$ gawk '                                                         # using GNU awk
@load "json"                                                     # load extension
{
    lines=lines $0                                               # read full JSON
    if(json_fromJSON(lines,data)!=0) {
        for( i in data["RELATIVES"])                             # iterate relatives
            printf "Relative name is %s from %s and phone# %s ", # output records
                data["RELATIVES"][i]["Name"], 
                data["RELATIVES"][i]["Country"], 
                data["RELATIVES"][i]["Phone"] 

        lines=""                                                 # rinse for repeat
    } 
}' file.json

Output as requested:

Relative name is Ram from India and phone# 111111111 Relative name is James from US and phone# 111111111 Relative name is Robin from UK and phone# 111111111 Relative name is Shyam from India and phone# 111111111 Relative name is Jon from US and phone# 111111111

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.