最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to query a json file? - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question edited Apr 12, 2016 at 11:24 Abdelkrim asked Apr 12, 2016 at 11:10 AbdelkrimAbdelkrim 2,1586 gold badges33 silver badges48 bronze badges 4
  • Are you okay with a mand-line solution? – Manish Maheshwari Commented Apr 25, 2019 at 12:42
  • You can use jq mand line tool for querying a json file. – Manish Maheshwari Commented Apr 30, 2019 at 18:00
  • 1 stedolan.github.io/jq – Manish Maheshwari Commented May 3, 2019 at 15:18
  • Similar question: Is there a query language for JSON? – Simon E. Commented Jun 14, 2021 at 9:11
Add a ment  | 

3 Answers 3

Reset to default 6

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))

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.

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;
  }
}
发布评论

评论列表(0)

  1. 暂无评论