---
title: "I cannot seem to get data into JSON correctly"  
description: "I cannot seem to get data into JSON correctly"  
author: "Anonymous User"  
published: 2013-05-02  
updated: 2013-05-02  
canonical: https://www.mindstick.com/forum/784/i-cannot-seem-to-get-data-into-json-correctly  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# I cannot seem to get data into JSON correctly

Hi Everyone!\
Hello I have spent quite bit of time [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to get my [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) to look like this in JSON as a sample:\
var json = { "id": "347_0", "name": "Nine Inch Nails", "[children](https://yourviews.mindstick.com/view/81386/mobile-app-based-education-curse-or-boon-for-our-children)": [{ "id": "126510_1", "name": "Jerome Dillon", "data": { "band": "Nine Inch Nails", "[relation](https://answers.mindstick.com/qa/99257/i-don-t-look-m-gandhi-in-the-same-way-as-britishers-do-relation-of-gandhi-with-them)": "member of band" }, "children": [{ "id": "52163_2", "name": "Howlin' Maggie", "data": { "band": "Jerome Dillon", "relation": "member of band" }, "children": [] }, { "id": "324134_3", "name": "nearLY", "data": { "band": "Jerome Dillon", "relation": "member of band" }, "children": [] }] }, { "id": "173871_4", "name": "Charlie Clouser", "data": { "band": "Nine Inch Nails", "relation": "member of band" }, "children": [] }, { "id": "235952_5", "name": "James Woolley", "data": { "band": "Nine Inch Nails", "relation": "member of band" }, "children": [] },\
I am using the json_encode [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) and I know how to make the data into JSON I just cannot seem to create anything that will [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) the above [JSON format](https://www.mindstick.com/forum/158266/what-is-the-json-format)...For example I use the following code:\
[foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally)($relations as $rel){ $data[$id]["relationTo"] = $rel["name"]; $data[$id]["relation"] = $rel["relation"]; $id = $id + 1; } $id = 0; foreach($relations as $rel){ $children[$id]["id"] = $id+1; $children[$id]["name"] = $rel["sname"]; $children[$id]["data"] = $data[$id]; $id = $id + 1; } $relationsArray["id"] = 0; $relationsArray["name"] = $rel["name"]; $relationsArray["children"] = $children;\
$json_content = json_encode($relationsArray);\
And this outputs:\
"id":0,"name":"Al","children":[ { "id":1, "name":"Brandon", "data":{ "relationTo":"Albaraa", "relation":"Friend" }, "children":[\
] }, { "id":2, "name":"Shen", "data":{ "relationTo":"Albaraa", "relation":"Friend" }, "children":[\
] }, { "id":3, "name":"Dan", "data":{ "relationTo":"Albaraa", "relation":"Professor" }, "children":[\
] }, { "id":4, "name":"Bob", "data":{ "relationTo":"Albaraa", "relation":"Boss" }, "children":[\
] }, { "id":5, "name":"Al", "data":{ "relationTo":"Albaraa", "relation":"God Father" }, "children":[\
] }, { "id":6, "name":"Albaraa", "data":{ "relationTo":"Shen", "relation":"Friend" }, "children":[\
] }, { "id":7, "name":"Brandon", "data":{ "relationTo":"Shen", "relation":"Friend" }, "children":[\
] }, { "id":8, "name":"Dan", "data":{ "relationTo":"Shen", "relation":"Professor" }, "children":[\
] }, { "id":9, "name":"Albaraa", "data":{ "relationTo":"Al", "relation":"God Son" }, "children":[\
] }, { "id":10, "name":"Bob", "data":{ "relationTo":"Al", "relation":"Best [Friends](https://answers.mindstick.com/qa/31396/where-did-the-suggest-to-friends-feature-go)" }, "children":[\
] } ]}and so on...but I am not able to get the children of the children as you see above!\
Any help would be amazing thank you!

## Replies

### Reply by AVADHESH PATEL

Hi Ankit!\
\
declared $children before assigning values to it before for each loop. Hence declare the array like this and alter the code to this\
\
$children = array();\
$id = 0;\
foreach($relations as $rel){\
$children[$id]["id"] = $id+1;\
$children[$id]["name"] = $rel["sname"];\
$children[$id]["data"] = $data[$id];\
$id = $id + 1;\
}\
\
I hope it resolve your problem.


---

Original Source: https://www.mindstick.com/forum/784/i-cannot-seem-to-get-data-into-json-correctly

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
