1

I have a very deep-down object from which I need to extract a specific object from it. but I don't quite know it.

This is a pic of object. enter image description here

As you see in the picture, I need to extract the slider and slides together.

Is there anyone who may be able to help a bit or just write down how to do it I will write the code out?

nst slide = `[{"data":{"pageLayouts":{"data":[{"attributes":{"title":"Home","subtitle":"Our project","slider":{"title":"She was undergoing treatment at Saifee Hospital in Mumbai for the past 15 day","slides":[{"id":"1","image":{"data":[{"id":"4","__typename":"UploadFileEntity"}],"__typename":"UploadFileRelationResponseCollection"},"title":"Prime Minister Narendra Modi","subtitle":"Prime Minis a Modi","__typename":"ComponentSlidesComSlides"},{"id":"3","image":{"data":[{"id":"5","__typename":"UploadFileEntity"}],"__typename":"UploadFileRelationResponseCollection"},"title":"Another slider","subtitle":"sub title for another slider","__typename":"ComponentSlidesComSlides"},{"id":"2","image":{"data":[{"id":"4","__typename":"UploadFileEntity"}],"__typename":"UploadFileRelationResponseCollection"},"title":"some more slider","subtitle":"sub title fore some more slider","__typename":"ComponentSlidesComSlides"}],"__typename":"ComponentSliderComSlider"},"__typename":"PageLayout"},"__typename":"PageLayoutEntity"}],"__typename":"PageLayoutEntityResponseCollection"}},"loading":false,"networkStatus":7,"stale":false}]`

Thank you so much.

phuzi
  • 12,078
  • 3
  • 26
  • 50
Divad
  • 273
  • 3
  • 14
  • Does this help? [Working with JSON](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON) – mykaf Jun 30 '22 at 14:46
  • A.b.c.d.e ... etc. Which object you want – Bravo Jun 30 '22 at 14:47
  • @user1599011 Please let me try it out, and thank you so much for the information much appreciated. – Divad Jun 30 '22 at 14:48
  • 4
    @user1599011 as is common, op is not actually dealing with JSON. Since a. They posted a picture of an object and b. There's no such thing as a JSON object – Bravo Jun 30 '22 at 14:48
  • 1
    _"This is a pic of the JSON."_ - Nope, that's not a picture of JSON, you appear to have a JavaScript object not a JSON formatted string. Please remove any references to JSON please. – phuzi Jun 30 '22 at 14:49
  • @Bravo "slider": – Divad Jun 30 '22 at 14:49
  • @phuzi it is a JSON object but I converted it into a proper Object, you can edit it as well in order to be more readable and if any correction may be needed. thank you for your time. – Divad Jun 30 '22 at 14:51
  • 2
    JSON is only ever a string. Never an object – Bravo Jun 30 '22 at 14:52
  • 1
    A "JSON Object" you mean a JSON formatted string? If you have already managed to convert the JSON to an object then the JSON part of the question is still not relevant. – phuzi Jun 30 '22 at 14:52
  • `X[0].data.pageLayouts.data[0].attributes.slider` where X is the variable storing the parsed JSON – Bravo Jun 30 '22 at 14:53
  • @phuzi Cool, thank you for making it clear I am new in js, and I do make mistakes. – Divad Jun 30 '22 at 14:53
  • @Bravo It would be nice to post it as an answer so I can check it out if it's ok, so we can make it as a correct answer and the other people can use it too. thanks so much. – Divad Jun 30 '22 at 14:56
  • 1
    Yes. It would be. But I'd post exactly what I put in the comment. Not sure how that would help more – Bravo Jun 30 '22 at 14:57

2 Answers2

1

var slider = obj[0].data.pageLayouts.data[0].attributes.slider

var obj = [{
  "data": {
    "pageLayouts": {
      "data": [{
        "attributes": {
          "title": "Home",
          "subtitle": "Our project",
          "slider": {
            "title": "She was undergoing treatment at Saifee Hospital in Mumbai for the past 15 day",
            "slides": [{
              "id": "1",
              "image": {
                "data": [{
                  "id": "4",
                  "__typename": "UploadFileEntity"
                }],
                "__typename": "UploadFileRelationResponseCollection"
              },
              "title": "Prime Minister Narendra Modi",
              "subtitle": "Prime Minis a Modi",
              "__typename": "ComponentSlidesComSlides"
            }, {
              "id": "3",
              "image": {
                "data": [{
                  "id": "5",
                  "__typename": "UploadFileEntity"
                }],
                "__typename": "UploadFileRelationResponseCollection"
              },
              "title": "Another slider",
              "subtitle": "sub title for another slider",
              "__typename": "ComponentSlidesComSlides"
            }, {
              "id": "2",
              "image": {
                "data": [{
                  "id": "4",
                  "__typename": "UploadFileEntity"
                }],
                "__typename": "UploadFileRelationResponseCollection"
              },
              "title": "some more slider",
              "subtitle": "sub title fore some more slider",
              "__typename": "ComponentSlidesComSlides"
            }],
            "__typename": "ComponentSliderComSlider"
          },
          "__typename": "PageLayout"
        },
        "__typename": "PageLayoutEntity"
      }],
      "__typename": "PageLayoutEntityResponseCollection"
    }
  },
  "loading": false,
  "networkStatus": 7,
  "stale": false
}]

var slider = obj[0].data.pageLayouts.data[0].attributes.slider
console.log(slider)
IT goldman
  • 14,885
  • 2
  • 14
  • 28
0

First covert JSON to JavaScript object and use object déstructuré syntax