Why does PHP json_encode ignore empty arrays?

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

There is no way to encode "an empty array" using PHP's extensions to the form url encoding syntax.,If you have an empty array then you don't have any values, so you don't get any key=value pairs.,You get a key=value pair for each value.,I need PHP to receive the json object as is, even if it contains empty arrays.

If you give it:

data: {
   foo: [1, 2, 3]
}

It will convert that to:

foo[] = 1 & foo[] = 2 & foo[] = 3
load more v
88%

Example #2 A json_encode() example showing some flags in use ,Example #1 A json_encode() example, To summarise, always test that your JSON decoder can handle the output you generate from json_encode(). ,json_decode() - Decodes a JSON string

{
   "a": 1,
   "b": 2,
   "c": 3,
   "d": 4,
   "e": 5
}
load more v
72%

Multidimensional arrays,Multidimensional arrays in PHP,Array: There are three types of array in PHP which are listed below:  ,How to delete an array element based on key in PHP?

load more v
65%

$object = (object) $array;
load more v
75%

The Elasticsearch API uses empty JSON objects in several locations which can cause problems for PHP. Unlike other languages, PHP does not have a "short" notation for empty objects and many developers are unaware how to specify an empty object.,This empty JSON object is what causes problems.,A common source of confusion with the client revolves around JSON arrays and objects, and how to specify them in PHP. In particular, problems are caused by empty objects and arrays of objects. This page shows you some common patterns used in Elasticsearch JSON API and how to convert that to a PHP representation.,We use the generic PHP stdClass object to represent an empty object. The JSON now encodes correctly.

{
   "query": {
      "match": {
         "content": "quick brown fox"
      }
   },
   "highlight": {
      "fields": {
         "content": {}
      }
   }
}
load more v
40%

In element names, transliterations such as < becoming "ampersand lt;" are not correct. The name escaping mechanism should be used. , Should the type attribute be on the root element?,Yes, to distinguish an object from an array. ,When an XForms processor receives a JSON object [ref JSON], it does the following:

XForms allows the initialization, processing and serialization of instances whose data come from a JSON source by transforming the JSON value into an XML instance, and serializing it back out as JSON. The XML version of JSON has been designed to be round-trippable, and to allow XPath selectors that resemble the equivalent Javascript selectors. For example, a JSON source like this

{
   "company": "example.com",
   "size": 50,
   "location": {
      "class": "international",
      "places": ["Amsterdam", "London"]
   }
}

is transformed to

<json>
   <company>example.com</company>
   <size type="number">50</size>
   <location>
      <class>international</class>
      <places starts="array">Amsterdam</places>
      <places>London</places>
   </location>
</json>

allowing formulations such as:

<instance src="http://example.com/company" />
<bind nodeset="location/places[1]" ... <submission resource="http://example.com/company" ...

To serialize an instance with a different media type, the attribute 'mediatype' should be added to the appropriate element. For instance to serialize a JSON instance as XML, write

< submission resource = "..."
mediatype = "application/xml"...

Similarly, to serialize an XML instance as JSON, write

< submission resource = "..."
mediatype = "application/json"...

Problem: You need to be able to differentiate between

 {
    "x": true
 }
 and {
    "x": "true"
 } {
    "x": 0
 }
 and {
    "x": "0"
 } {
    "x": null
 }
 and {
    "x": "null"
 }


Version: Strings get marked specially

{"height": 1080, "width": 1920} 
	<json><height>1080</height><width>1920</width></json> 
{"name": "Mark", "age": 21}     
	<json><name type="string">Mark</name><age>21</age></json> 
{"selected": true}              
	<json><selected>true</selected></json> 
{"load": [0.31, 0.33, 0.32]}    
	<json><load array="true">0.31</load><load array="true">0.33</load><load array="true">0.32</load></json> 
{"cities": ["Amsterdam", "Paris", "London"]} 	 
	<json><cities array="true" type="string">Amsterdam</cities><cities array="true" type="string">Paris</cities><cities array="true" type="string">London</cities></json> 
{"left": {"x": 0, "y": 0}, "right": {"x": 100, "y": 100}} 	 
	<json><left><x>0</x><y>0</y></left><right><x>100</x><y>100</y></right></json> 
{"p": null}                      
	<json><p/></json> 

Version: non-strings get marked specially

{"height": 1080, "width": 1920} 
	<json><height type="number">1080</height><width type="number">1920</width></json> 
{"name": "Mark", "age": 21}     
	<json><name>Mark</name><age type="number">21</age></json> 
{"selected": true}              
	<json><selected type="boolean">true</selected></json> 
{"load": [0.31, 0.33, 0.32]}    
	<json><load array="true" type="number">0.31</load><load array="true" type="number">0.33</load><load array="true" type="number">0.32</load></json> 
{"cities": ["Amsterdam", "Paris", "London"]} 	 
	<json><cities array="true">Amsterdam</cities><cities array="true">Paris</cities><cities array="true">London</cities></json> 
{"left": {"x": 0, "y": 0}, "right": {"x": 100, "y": 100}} 	 
	<json><left><x type="number">0</x><y type="number">0</y></left><right><x type="number">100</x><y type="number">100</y></right></json> 
{"p": null}                      
	<json><p type="null"/></json> 

One suggestion for the name of the child element is "item". Another is to use the no-name element name, double underscore. From an XPath referencing perspective, an author would just use /* either way to get to the level down. Here's the company example again with some of these changes:

{
   "company": "example.com",
   "size": 50,
   "location": {
      "class": "international",
      "places": ["Amsterdam", "London"]
   }
}

is transformed to

<root type="object>
      <company>example.com</company>
      <size type=" number">50</size>
   <location type="object">
      <class>international</class>
      <places type="array">
         <__>Amsterdam</__>
         <__>London</__>
      </places>
   </location>
</root>

allowing references such as

< bind nodeset = "location/places/*[1]"...
<root type="array">
   <__ type="array">
      <__ type="number">1</__>
      <__ type="number">2</__>
   </__>
   <__ type="array">
      <__ type="number">3</__>
      <__ type="number">4</__>
   </__>
</root>

Although XForms doesn't define transformations for other mediatypes, implementors are encouraged to. As with JSON, ideally the mapping and serialization should be opaque to the form. For instance a mapping for VCARD (to take an example) could look like this:

     BEGIN: VCALENDAR
     METHOD: PUBLISH
     PRODID: - //Example/ExampleCalendarClient//EN
        VERSION: 2.0
     BEGIN: VEVENT
     ORGANIZER: mailto: a @example.com
     DTSTART: 19970701 T200000Z
     DTSTAMP: 19970611 T190000Z
     SUMMARY: ST.PAUL SAINTS - VS - DULUTH - SUPERIOR DUKES
     UID: 0981234 - 1234234 - 23 @example.com
     END: VEVENT
     END: VCALENDAR

can be transformed to

     <VCALENDAR>
        <METHOD>PUBLISH</METHOD>
        <PRODID>-//Example/ExampleCalendarClient//EN</PRODID>
        <VERSION>2.0</VERSION>
        <VEVENT>
           <ORGANIZER>mailto:a@example.com</ORGANIZER>
           <DTSTART>19970701T200000Z</DTSTART>
           <DTSTAMP>19970611T190000Z</DTSTAMP>
           <SUMMARY>ST. PAUL SAINTS -VS- DULUTH-SUPERIOR DUKES</SUMMARY>
           <UID>0981234-1234234-23@example.com</UID>
        </VEVENT>
     </VCALENDAR>
load more v
22%

The JSON representation for a resource is based on the JSON format described in STD 90 (RFC 8259) , and is described using this format: ,Just as in XML, JSON objects and arrays are never empty, and properties never have null values (except for a special case documented below). Omit a property if it is empty,There are no namespaces in the JSON representation, This specification defines the following method for canonicalizing FHIR resources, when represented as JSON:

The JSON representation for a resource is based on the JSON format described in STD 90 (RFC 8259) , and is described using this format:

{
   "resourceType": "[Resource Type]",
   // from Source: property0
   "property1": "<[primitive]>", // short description
   "property2": {
      [Data Type]
   }, // short description
   "property3": { // Short Description
      "propertyA": {
         CodeableConcept
      }, // Short Description (Example)
   },
   "property4": [{ // Short Description
      "propertyB": {
         Reference(ResourceType)
      } // R!  Short Description
   }]
}
load more v

Other "undefined-undefined" queries related to "Why does PHP json_encode ignore empty arrays?"