Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 306
Abdelkrim Avatar asked Dec 15 '22 07:12

Abdelkrim


1 Answers

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))
like image 90
Emil S. Jørgensen Avatar answered Dec 17 '22 01:12

Emil S. Jørgensen