-1

How can I search the keyword from JSON format. using jquery or javascript

I have a JSON content. From that I need to search and get the id for the searched keyword.

Keyword: "authority" = > I want to get id as 4

Keyword: "basic" = > I want to get id as 3

{
 "data": {
  "1":[
    {
    "id":"3",
    "title":"my title",
    "content":"this is very basic sample content"
    },
    {
    "id":"4",
    "title":"My sample title and text",
    "content":"renewed by a licensing authority may be signed by such officer by the State Government.<p></p>"
    }
  ]
 }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
subha s
  • 3
  • 1
  • 2

1 Answers1

0
        $(document).ready(function(){
        var content = {
         "data": {
          "1":[
            {
            "id":"3",
            "title":"my title",
            "content":"this is very basic sample content"
            },
            {
            "id":"4",
            "title":"My sample title and text",
            "content":"renewed by a licensing authority may be signed by such officer by the State Government.<p></p>"
            }
          ]
         }
         };

        alert(returnContent("basic"));

        function returnContent(keyword)
        {
            var returnValue = null;
            $.each(content.data, function(key, value){
                $.each(value, function(key2, value2){
                    if(value2.content.toString().indexOf(keyword) != -1)
                    {
                        returnValue = value2.id.toString();
                    }
                });
            });
            return returnValue;
        }


    });

You only have to change the keyword you are looking for in the returnContent() function

Simon Paquet
  • 615
  • 5
  • 11