Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json fieldnames spaces

I've such a json structure:

info:
{
First Name: "Robert",
Last Name: "Smith"
}

I'm tring to point to data with javascript using something like: "info.First Name" I know it's incorrect. How can I retrieve those information from the structure I have?

thank

like image 690
Lopoc Avatar asked Apr 19 '11 13:04

Lopoc


People also ask

Can JSON fields have spaces?

JSON Simple Array ExamplesWhitespace (Space, Horizontal tab, Line feed or New line or Carriage return) does not matter in JSON. It can also be minified with no affect to the data.

Can JSON object keys have spaces?

We typically write JavaScript object properties using camelCase, without spaces, but you can use spaces in keys if you prefer. Just be sure to include quotation marks to specify the string you're using as the object key.

Can JSON have spaces in property names?

That's not valid JSON. JSON is a data transport format that requires field names to be string delimited with double quotes, e.g.

How do you put a space in a JSON file?

You can use \u0020 to escape a space in JSON.

How do you comment inside a JSON?

To do this, you need to add an element to your JSON file, such as "_comment," which will contain your comment. The JSON API endpoint must ignore this particular JSON comment element. In this JSON comment example, we have included two comment elements in the JSON data, which you can see below.


1 Answers

That's not valid JSON. JSON is a data transport format that requires field names to be string delimited with double quotes, e.g.

{
    "info" : {
        "First Name": "Robert",
        "Last Name": "Smith"
    }
}

After parsing, you can then use obj.info["First Name"] to access the First Name field.

What you have is a JS object literal (that's still invalid), but you can apply the same technique (stringify the property names) to reach the same end goal.

like image 127
Andy E Avatar answered Sep 23 '22 01:09

Andy E