JSON: is this a json array? - arrays

"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
I know that this is a json array.
I want to do something like this:
"employees":[
"manager":{"firstName":"John", "lastName":"Doe"},
"boss1":{"firstName":"Anna", "lastName":"Smith"},
"boss2":{"firstName":"Peter","lastName":"Jones"}
]
Is this json format? Is this a json array?

No, this is not JSON array, you can check it on this link https://www.jsoneditoronline.org/ just past your code on the left side, and surround it with {} , and you will see where the problem is. Also you can test JSON there in the future.
My suggestion is to use something like this.
"employees":[
{"position":"manager", "firstName":"John", "lastName":"Doe"},
{"position":"boss1", "firstName":"Anna", "lastName":"Smith"},
{"position":"boss2", "firstName":"Peter","lastName":"Jones"}
]
This would be valid JSON array.

If you add some { and } you can make it valid jsons, like this:
{"employees":[
{"manager":{"firstName":"John", "lastName":"Doe"}},
{"boss1":{"firstName":"Anna", "lastName":"Smith"}},
{"boss2":{"firstName":"Peter","lastName":"Jones"}}
]}
I tested it on https://www.jsoneditoronline.org/ and this is valid. Without the need to add the extra "position".

Related

How to get the first item using JSONPath resulting array?

I have a JSON similar to:
{
"orders":{
"678238": {
"orderId": 678238,
"itemName": "Keyboard"
},
"8723423": {
"orderId": 8723423,
"itemName": "Flash Drive"
}
}
}
I am trying JSON path to get first orderId. When I try $..orderId I get an array listing both orderId, then I tried $..[0].orderId to get first item from that array (following JsonPath - Filter Array and get only the first element). But it does not work. I am confused.
try this
console.log(jsonPath(json,"$['orders'].[orderId]")[0]); //678238
You're almost there. You need to combine the two things you've done.
$..orderId[0]
The ..orderId recursively searches for orderId properties, giving you all of their values, as you mentioned. Taking that result, you just need to apply the [0].
Be careful, though. Because your data is an object, keys are unordered, so the first one in the JSON text may not be the first one encountered in memory. You'll want to do some testing to confirm your results are consistent with your expectations.
Your JSON doesn't even have an array and you are expecting to get first item from the array which is why it's not working.
Suppose, if the structure of JSON is modified like this
{
"orders": [{
"orderId": 678238,
"itemName": "Keyboard"
},
{
"orderId": 8723423,
"itemName": "Flash Drive"
}
]
}
then you can use the query to get the first order.
$.orders[0].orderId

Angularjs fetching data

I'm new to angularjs. I have a json.
{
"first": {
"content": {
"admin.nv.example.com": {
"start": "2016-02-24 06:04:12.772141"
}
}
}
}
This is my JSON response i want to obtain value of property name (start) in this whole response but whenever i try to access this property value by first.content.admin.example.com.start
it shows error:Cannot read property 'nv' of undefined(angularjs)
You should use a different syntax to access the dotted property name. In fact it is better to avoid names like 'admin.nv.example.com' containing dots.
This should work:
first.content['admin.example.com'].start
use first.content['admin.nv.example.com']
You should use the ["admin.nv.example.com"] notation.
You should understand the proper way of writing JSON. But you should use like this:
test['first']['content']['admin.nv.example.com'];
JSON formats

Get each JSON array value using PHP

I've been trying to read this JSON with PHP:
{
"total_found":"49",
"1":{
"title":"Maze Runner-The Scorch Trials 2015 HD-TS x264-Garmin",
"category":"video",
"seeds":2141,
"leechs":1176,
"torrent_size":1122230176,
"torrent_hash":"29d3e7062825a62abdd877ee96dc3fea41183836"
},
"2":{
"title":"Maze.Runner-The.Scorch.Trials.2015.720P.HD-TS.x264.AC3.HQ.Hive-CM8",
"category":"hdrip",
"seeds":395,
"leechs":1856,
"torrent_size":3999751475,
"torrent_hash":"e1e14a5ccf540a739907db2e1e0d810ecdb8bebc"
}
}
How can I get each title and torrent_size?
The easiest thing to do is to decode the JSON to an array. PHP has a built-in function for this:
$results = json_decode($json_data);
http://php.net/manual/en/function.json-decode.php
To clearly see the array structure, try dumping the variable $results using print_r();, i.e.:
print_r($results);
Then you simply need to access the data using the array.
It will be something like $results[1]['torrent_size'];.

Pass an array of integers to ElasticSeach template

I am trying to pass an array of integers to ElasticSearch template using the below mustache template.
{{#filter5_terms}}
"terms": {
"{{filter5_name}}": [
"{{#filter5_lt}}",
"{{.}}",
"{{/filter5_lt}}" ]
}
{{/filter5_terms}}
Above works, If I pass a string array (Ex: ["A","B"]. But the same is failing with the int array [1,2] with Nested: NumberFormatException[For input string: ""]; error.
Reference: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html#_passing_an_array_of_strings
Can you please let me know if I am missing anything?
Thanks
Anil
You really shouldn't rely on that, as the format is an inner implementation of Mustache and thus, subject to change. For example, if you try to emulate that using mustache.js, you'll get something like:
"terms: {
"property": 3,4
}
To workaround this problem, you should add square brackets to the templated values. So, your example becomes:
"terms": {
"{{filter5_name}}": [{{filter5_lt}}]
}
And that will get you what you want.
At least, this is true in mustache.js 2.2.1
I did fix this.
We can use the below to replace the integer array into ElasticSearch query.
"terms": {
"{{filter5_name}}": {{filter5_lt}}
}
ElasticSearch documentation has an example to replace string arrays and I tried to use the same for integer arrays and it did not work.
So I had to use the above which is provided in Mustache templating examples.
Thanks
Anil

Convert json string to array in php

Im using curl the retrieve a json string from a log file. I was able to retrieve the string but it comes in this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"}]
[{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
When i try to convert it to an array using decode, its as if decode isn't working. I was hoping someone could assist me in figuring this out.
First, this json seems to be wrong. It should has this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
Maybe it's the problem.
#Carlos is right your JSON is invalid. You have 2 separate arrays instead of an array of objects.
It should look like this instead:
[
{
"candy": "lollipop",
"timestamp": 1385504260,
"color": "red"
},
{
"candy": "laffytaffy",
"timestamp": 1385504260,
"color": "blue"
}
]
Try like this
<?php
$jsonData = '[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]';
$decodedJson = array();
$decodedJson = json_decode($jsonData);
print_r ($decodedJson);
?>

Resources