0

How do I retrieve the list of objects under the entities and ministries keys respectively in javascript from the sample JSON string provided? Your help will surely be appreciated.

Note: The list was created from a PHP script using the "json_encode" function

Sample JSON String

[
  {
    "entities":[
      {
        "id":5,
        "entity_name":"Limited",
        "ministry_id":5,
        "entity_description":"Technology providers",
        "created_at":"2019-05-01 00:00:00",
        "updated_at":null
      },
      {
        "id":6,
        "entity_name":"eLearning",
        "ministry_id":1,
        "entity_description":"Provides an educational portal for students",
        "created_at":"2019-05-01 00:00:00",
        "updated_at":null
      }
    ],
    "ministries":[
      {
        "id":5,
        "name":"Science"
      },
      {
        "id":1,
        "name":"Finance"
      }
    ]
  }
]
f-CJ
  • 4,235
  • 2
  • 30
  • 28
Flavah
  • 103
  • 7
  • Javascript `JSON.parse(string)` is the inverse to the PHP `json_encode(object)` – Taplar May 01 '19 at 22:52
  • 4
    Possible duplicate of [Safely turning a JSON string into an object](https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – Taplar May 01 '19 at 22:55
  • It depends on how you are returning the JSON. If you are using AJAX then you can set the dataType to handle this for you. If you are echoing it into a JS variable on a page, then you can access it directly, given you have echoed it properly. – Second2None May 01 '19 at 23:03

1 Answers1

1

var data = `
[{
    "entities": [{
            "id": 5,
            "entity_name": "Limited",
            "ministry_id": 5,
            "entity_description": "Technology providers",
            "created_at": "2019-05-01 00:00:00",
            "updated_at": null
        },
        {
            "id": 6,
            "entity_name": "eLearning",
            "ministry_id": 1,
            "entity_description": "Provides an educational portal for students",
            "created_at": "2019-05-01 00:00:00",
            "updated_at": null
        }
    ],
    "ministries": [{
            "id": 5,
            "name": "Science"
        },
        {
            "id": 1,
            "name": "Finance"
        }
    ]
}]
`

var obj = JSON.parse(data)[0]
console.log(obj)
console.log(obj.entities[0])

JSON.parse(data) will convert the JSON to an object.

Read more on MDN

Wendelin
  • 2,354
  • 1
  • 11
  • 28
  • This is an easy google, and undoubtedly has many duplicates on the site. – Taplar May 01 '19 at 22:53
  • I totally agree but I'm not an asshole so I'll just answer his question. It will be closed eventually anyway. – Wendelin May 01 '19 at 22:56
  • Not answering duplicate questions is not being an asshole. It's being a productive maintainer of the website. We close duplicates for a reason. There's no point in having the same information all over the place. Especially when searching "javascript, parse json" **on site** turns up existing questions. – Taplar May 01 '19 at 22:57
  • As I said I agree, I even flagged his question as a duplicate but I thought I would save him a few minutes by answering it directly – Wendelin May 01 '19 at 22:59
  • This would be wrong if he was echoing it onto the page for eg: `var jsonObj= ;` – Second2None May 01 '19 at 23:05
  • @Second2None I am not sure what you mean. The question never mentions how the string is provided. I used a string literal (`data`) as a placeholder. And if he does echo it onto the page then he just has to add quotes like this: var jsonString = ``; – Wendelin May 01 '19 at 23:09
  • Thank you very much, Wendelin. Just what I've been looking for. – Flavah May 01 '19 at 23:10
  • @MarvinMullings I am happy that I was able to help you. You should mark the answer as correct then. – Wendelin May 01 '19 at 23:11