0

I getting a json from a url but it has lots of objects, how would I only get the slugs giving the json provided below?

I don't mind it either in php or js, I mean on the one hand I need to know how to get what object I want using php or js, on the other hand I am wondering if we can get an object providing a parameter in the url

[  
   {  
      "id":580,
      "count":0,
      "description":"qwer",
      "link":"http:\/\/example.com\/ing\/category\/persone\/414qwer1324r-qewr1233423\/",
      "name":"414qwer1324r qewr1233423",
      "slug":"414qwer1324r-qewr1233423",
      "taxonomy":"category",
      "parent":183,
      "meta":[  

      ],
      "_links":{  
         "self":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/580"
            }
         ],
         "collection":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"
            }
         ],
         "about":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"
            }
         ],
         "up":[  
            {  
               "embeddable":true,
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"
            }
         ],
         "wp:post_type":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=580"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   },
   {  
      "id":586,
      "count":1,
      "description":"asdfasd",
      "link":"http:\/\/example.com\/ing\/category\/persone\/add-person\/",
      "name":"Add person",
      "slug":"add-person",
      "taxonomy":"category",
      "parent":183,
      "meta":[  

      ],
      "_links":{  
         "self":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/586"
            }
         ],
         "collection":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"
            }
         ],
         "about":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"
            }
         ],
         "up":[  
            {  
               "embeddable":true,
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"
            }
         ],
         "wp:post_type":[  
            {  
               "href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=586"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   },
   {  
      "id":520,
      "count":8,
      "description":"",
      "link":"http:\/\/example.com\/ing\/category\/argomenti\/amici\/",
      "name":"AMICI",
      "slug":"amici",
      "taxonomy":"category",
      "parent":184,
      "meta":[  

How would I get a list of all "slug":" with their value like "slug":"amici" in a url?

Like http://example.com/ing/wp-json/wp/v2/categories/names

2
  • Are you referring to the property in each object named "slug"? Commented Feb 17, 2019 at 17:18
  • @guest271314 yes I am Commented Feb 17, 2019 at 17:19

3 Answers 3

1

In JavaScript you can use Array.prototype.map() and destructuring assignment

let res = data.map(({slug}) => slug)
Sign up to request clarification or add additional context in comments.

5 Comments

where would you define the URL and what Is it {slug}, didn't know about that
@rob.m What URL? What do you mean by "define"? See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
yeah I mean the DATA, I need to get data from a url from another web site with REST api
@rob.m What data? This is the question at OP "how would I only get the slugs giving the json provided below?"
@rob.m Not following what you mean. The code at the answer gets all values of properties named "slug" and returns an Array containing those values
1

You can use map and destructuring assignment

let data = [{"id":580,"count":0,"description":"qwer","link":"http:\/\/example.com\/ing\/category\/persone\/414qwer1324r-qewr1233423\/","name":"414qwer1324rqewr1233423","slug":"414qwer1324r-qewr1233423","taxonomy":"category","parent":183,"meta":[],"_links":{"self":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/580"}],"collection":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"}],"about":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"}],"up":[{"embeddable":true,"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"}],"wp:post_type":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":586,"count":1,"description":"asdfasd","link":"http:\/\/example.com\/ing\/category\/persone\/add-person\/","name":"Addperson","slug":"add-person","taxonomy":"category","parent":183,"meta":[],"_links":{"self":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/586"}],"collection":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories"}],"about":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/taxonomies\/category"}],"up":[{"embeddable":true,"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/categories\/183"}],"wp:post_type":[{"href":"http:\/\/example.com\/ing\/wp-json\/wp\/v2\/posts?categories=586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]

let op = data.map(({slug})=> slug )

console.log(op)

4 Comments

what about all the slugs and not only one slug?
@rob.m what do you mean by that ? code here giving you all the value with key slug.
ehmm i try this on here but it only shows me [ "414qwer1324r-qewr1233423", "add-person" ]
haha, sorry, had placed only part of the json! SO obviously there is only one haha, thanks
0

In PHP you can do it with array_column function:

$slugs = array_column($posts, 'slug');

2 Comments

i mean are you suggesting to get the json as a url like $posts = myUrl and then run that?
You can try $posts[array_search('yourUrl', array_column($posts, 'slug'))] to get needed post. To decode your string to $posts array, use $posts = json_decode($_POST['posts'], true);

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.