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"
      }
    ]
  }
]
3
  • Javascript JSON.parse(string) is the inverse to the PHP json_encode(object) Commented May 1, 2019 at 22:52
  • 4
    Possible duplicate of Safely turning a JSON string into an object Commented May 1, 2019 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. Commented May 1, 2019 at 23:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

This is an easy google, and undoubtedly has many duplicates on the site.
I totally agree but I'm not an asshole so I'll just answer his question. It will be closed eventually anyway.
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.
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
This would be wrong if he was echoing it onto the page for eg: var jsonObj= <?php echo json_encode( $json); ?>;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.