---
title: "How to add attribute in JSON in PHP?"  
description: "How to add attribute in JSON in PHP?"  
author: "Kate Smith"  
published: 2013-06-20  
updated: 2013-06-20  
canonical: https://www.mindstick.com/forum/1250/how-to-add-attribute-in-json-in-php  
category: "json"  
tags: ["json"]  
reading_time: 1 minute  

---

# How to add attribute in JSON in PHP?

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs),\
I have [Json](https://www.mindstick.com/forum/34446/convert-json-string-to-object) in [PHP](https://www.mindstick.com/articles/12149/php-constant-and-php-variables) like this:

```
$json = '{"total":"100", "page":"1", "records":"100", "rows": [ {"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';
```

\
and I want to add\
"test1":"5","test2":"7"into JSON above.\
so it will be like this:\

```
$json = '{"total":"100", "page":"1", "records":"100", "rows": [     {"no":"1","part_number":"2","part_name":"3","price":"4","test1":"5","test2":"7","note":"8"}]}';
```

\
Please, help me. How to [add attribute](https://www.mindstick.com/forum/2075/add-attribute-to-the-body-failed) in JSON in PHP?

## Replies

### Reply by AVADHESH PATEL

Hi, \
Try as following \
\
**$json = '{"total":"100", "page":"1", "records":"100", "rows": [** **{"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';**\
// decode json**$json = json_decode($json);**\
// [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) data**$json->rows[0]->test1 = "5";****$json->rows[0]->test2 = "7";**\
// echo it for testing puproses**print_r($json);**// re-encode **$json = json_encode($json);****echo $json;****\****\**\


---

Original Source: https://www.mindstick.com/forum/1250/how-to-add-attribute-in-json-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
