Abdelkrim
Abdelkrim

Reputation: 2158

How to query a json file?

Here is the file I would like to parse

  1. I receive a file from a webservice in JSON format.
  2. I would like to parse the content in such a way that I display the name of the president from the USA
{
    "response": {
        "result": {
            "Countries": {
                "row": [
                    {
                        "no": "1",
                        "FL": [
                            {
                                "content": "USA",
                                "val": "Country"
                            },
                            {
                                "content": "Barack Obama",
                                "val": "President"
                            }
                        ]
                    },
                    {
                        "no": "2",
                        "FL": [
                            {
                                "content": "Cuba",
                                "val": "Country"
                            },
                            {
                                "content": "Raul Castro",
                                "val": "President"
                            }
                        ]
                    }
                ]
            }
        }
    }
}

The expected output

{ presidents: [
    { "name": "Barack Obama"}
    ]
}

could you provide a solution using a kind of JSON XPath?

Upvotes: 4

Views: 32481

Answers (3)

cyberbrain
cyberbrain

Reputation: 5135

Despite of the age of the question I want to add this answer as reference for future visitors with the same problem:

You can use JSONPath. The page contains a description and an implementation in JavaScript and PHP.

Upvotes: 4

Emil S. Jørgensen
Emil S. Jørgensen

Reputation: 6366

Assuming that you are loading the response into a variable data:

var data = {
    "response" : {
        "result" : {
            "Countries" : {
                "row" : [{
                        "no" : "1",
                        "FL" : [{
                                "content" : "USA",
                                "val" : "Country"
                            }, {
                                "content" : "Barack Obama",
                                "val" : "President"
                            }
                        ]
                    }, {
                        "no" : "2",
                        "FL" : [{
                                "content" : "Cuba",
                                "val" : "Country"
                            }, {
                                "content" : "Raul Castro",
                                "val" : "President"
                            }
                        ]
                    }
                ]
            }
        }
    }
};

You can then filter your data like this:

data.response.result.Countries.row.filter(function (el) {
    return (el.FL[0].content == "USA");
})[0].FL[1];

To get to:

{
    "content" : "Barack Obama",
    "val" : "President"
}

To get the name, simply specify "content"

data.response.result.Countries.row.filter(function(el){
  return (el.FL[0].content == "USA");
})[0].FL[1].content;

EDIT 1

One could search a json object like a string.

If we know that the element will have no children, then we could use something like this:

function find(query,obj) {
  var str = JSON.stringify(obj);
  var start = str.substr(0,str.indexOf(query)).lastIndexOf('{');
  var end = str.substr(start,str.length).indexOf('}');
  return str.substr(start,end);
}

console.log(find('"content":"USA"',data))

Upvotes: 6

AlmasK89
AlmasK89

Reputation: 1340

t = {
    "response": {
        "result": {
            "Countries": {
                "row": [
                    {
                        "no": "1",
                        "FL": [
                            {
                                "content": "USA",
                                "val": "Country"
                            },
                            {
                                "content": "Barack Obama",
                                "val": "President"
                            }
                        ]
                    },
                    {
                        "no": "2",
                        "FL": [
                            {
                                "content": "Cuba",
                                "val": "Country"
                            },
                            {
                                "content": "Raul Castro",
                                "val": "President"
                            }
                        ]
                    }
                ]
            }
        }
    }
}
res={};//Here we will store result
for (i in t.response.result.Countries.row) {
  // get current country
  country = t.response.result.Countries.row[i].FL[0].content;
  // get current president
  president = t.response.result.Countries.row[i].FL[1].content;
  if (country == 'USA') {
    res.presidents=[{name:president}];
    break;
  }
}

Upvotes: 3

Related Questions