Question

I am looking for this type of text in a json file:

{"id":"*****number*******","name":"****namehere******"}

For example:

{"id":"100007015692915","name":"Ida Ka"}

The JSON file is a complicated JSON file that looks like this (this is part of the JSON file):

updated_time":"2014-02-19T15:15:31+0000","likes":{"data":[{"id":"1487307502","name":"Svatava Dohnalov\u00e1"},{"id":"662920773","name":"Alena Al Hr\u010dkov\u00e1"},{"id":"100007015692915","name":"Ida Ka"},{"id":"100000533191263","name":"Zlati V\u00e1\u0148ov\u00e1"},{"id":"1658256962","name":"Ji\u0159\u00ed Jager"},{"id":"1054706643","name":"Pavla Bubikova Winter"}],"paging":

How can I recover all of these names and ids from a JSON file, and put them in an array?

I am using PHP and the solution would include regex probably.

Was it helpful?

Solution

You can use this regex:

"id":"(\d+)","name":"(.*?)"

Check the capturing groups in this working demo

MATCH 1
1.  [65-75] `1487307502`
2.  [85-107]    `Svatava Dohnalov\u00e1`
MATCH 2
1.  [117-126]   `662920773`
2.  [136-162]   `Alena Al Hr\u010dkov\u00e1`
MATCH 3
1.  [172-187]   `100007015692915`
2.  [197-203]   `Ida Ka`
MATCH 4
1.  [213-228]   `100000533191263`
2.  [238-265]   `Zlati V\u00e1\u0148ov\u00e1`
MATCH 5
1.  [275-285]   `1658256962`
2.  [295-315]   `Ji\u0159\u00ed Jager`
MATCH 6
1.  [325-335]   `1054706643`
2.  [345-366]   `Pavla Bubikova Winter`

OTHER TIPS

You're looking for json_decode.

$json = '{"id":"100007015692915","name":"Ida Ka"}';
$arr = json_decode($json, true);

print_r($arr);

Returns:

Array ( [id] => 100007015692915 [name] => Ida Ka )

$data = json_decode($json, true);
$like_data = $data['likes']['data'];
foreach($like_data as $like) {
    echo "ID: {$like['id']} Name: {$like['name']}<br/>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top