JSON Array Structure Variations - json

Below are 3 JSON Array structure formats...
The first one, the one outlined at JSON.org, is the one I am familiar with:
Format #1
{"People": [
{
"name": "Sally",
"age": "10"
},
{
"name": "Greg",
"age": "10"
}
]}
The second one is a slight variation that names the elements of the array. I personally don't care for it; you don't name elements of an array in code (they are accessed by index), why name them in JSON?
Format #2
{"People": [
"Person1": {
"name": "Sally",
"age": "10"
},
"Person2": {
"name": "Greg",
"age": "10"
}
]}
This last one is another variation, quite similar to Format #2, but I have a hunch this one is incorrect because it appears to have extra curly braces where they do not belong.
Format #3
{"People": [
{
"Person1": {
"name": "Sally",
"age": "10"
}
},
{
"Person2": {
"name": "Greg",
"age": "10"
}
}
]}
Again, I'm confident that Format #1 is valid as it is the JSON Array format outlined at JSON.org. However, what about Format #2 and Format #3? Are either of those considered valid JSON? If yes, where did those formats come from? I do not see them outlined at JSON.org or on Wikipedia.

Both #1 and #3 are (nearly - there are commas missing) valid JSON, but encode different structures:
#1 gives you an Array of Objects, each with name and age String properties
#3 gives you an Array of Objects, each with a single Object property, each with name and age String properties.
The #2 is invalid: Arrays (as defined by [ ... ]) may not contain property names.

Solution For Format#1
By default:
array=[];
object={};
JSON Code:
var Json = {
People:[]
};
Json.People.push({
"name": "Sally",
"age": "10"
});
Json.People.push({
"name": "Greg",
"age": "10"
});
JSON Result:
{"People":
[
{
"name": "Sally",
"age": "10"
},
{
"name": "Greg",
"age": "10"
}
]
}

Related

How to read nested array elements from JSON?

I need to parse a JSON with nested array elements and extract the values.
I am not sure how to use the nested array to set the value of an attribute in output JSON.
This is the input:
[{
"name": "book1",
"id": 18789,
"locations": [{
"state": "mystate",
"phone": 8877887700
}, {
"state": "mystate1",
"phone": 8877887701
}]
},
{
"name": "book2",
"id": 18781,
"locations": [{
"state": "mystate3",
"phone": 8877887711
}, {
"state": "mystate4",
"phone": 8877887702
}]
}]
And this is the expected output:
{
"name": ["book1", "book2"],
"id": ["18789", "18781"],
"states": [
["mystate", "mystate"],
["mystate3", "mystate4"]
]
}
I am trying to use the following JSLT expression:
{
"name" : [for (.)
let s = string(.name)
$s],
"id": [for (.)
let s = string(.id)
$s],
"states": [for (.)
let s = string(.locations)
$s]
}
But I am not sure how to set the states in this case so that I have the value of state in the output.
A solution using JQ or JSONPath may also help.
With JQ it'd be easier than that.
{
name: map(.name),
id: map(.id),
states: map(.locations | map(.state))
}
Online demo
In JSLT you can implement it like this:
{
"name" : [for (.) .name],
"id": [for (.) .id],
"states": flatten([for (.) [for (.locations) .state]])
}
The states key is a bit awkward to implement, as you see. I have thought of making it possible to let path expressions traverse arrays, and if we add that to the language it could be implemented like this:
{
"name" : .[].name,
"id": .[].id,
"states": .[].locations.[].state
}

Accessing Json Array of objects in React-Native

I'm able to call parcels using map as list.parcels and the output comes like
{parcelId: "12"}
{parcelId: "14"}
{parcelId: "15"}
{parcelId: "16"}
{parcelId: "17"}
but i need to print only parcelId value, so to do that I tried list.parcels.parcelId and it shows undefined. I need help in printing the parcelId alone. Thanks in advance.
Json is as below:
{
"id": "7",
"type": "history",
"title": "History Parcel Delivery",
"sender": "Paul",
"price": "635.50",
"parcels": [
{
"parcelId": "12"
},
{
"parcelId": "14"
},
{
"parcelId": "15"
},
{
"parcelId": "16"
},
{
"parcelId": "17"
}
]
}
Since, list.parcels is an array you must use an index to reference an object within it. So to refer to the first item in the array you should use list.parcels[0].parcelId. This will print "12". So to print a specific parcelId use:
list.parcels[i].parcelId
where value of i is between 0 and one less than the length of the array
If you just need a list of the parcelId you can use
parcelIds = lists.parcels.map((parcel) => parcel.parcelId);
i know it to much late,but i hope its helpfull for others,How to Accessing Json Array of objects in React-Native
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
var cardatafetch=[]
for (i in myObj.cars) {
var dataa= myObj.cars[i];
//and for push the array data into array var,i declare it above the for loop you can declare any where
cardatafetch.push(dataa)
}
console.log(cardatafetch)

Jolt reference first element in array as target name

I have been looking at this for a few weeks (in the background) and am stumped on how to convert JSON data approximating a CSV into a tagged set using the NiFi JoltTransformJson processor. What I mean by this is to use the data from the first row of an array in the input as the JSON object name in the output.
As an example I have this input data:
[
[
"Company",
"Retail Cost",
"Percentage"
],
[
"ABC",
"5,368.11",
"17.09%"
],
[
"DEF",
"101.47",
"0.32%"
],
[
"GHI",
"83.79",
"0.27%"
]
]
and what I am trying to get as output is:
[
{
"Company": "ABC",
"Retail Cost": "5,368.11",
"Percentage": "17.09%"
},
{
"Company": "DEF",
"Retail Cost": "101.47",
"Percentage": "0.32%"
},
{
"Company": "GHI",
"Retail Cost": "83.79",
"Percentage": "0.27%"
}
]
I see this as primarily 2 problems: getting access to the content of the first array and then making sure that the output data does not include that first array.
I would love to post a Jolt Specification showing myself getting somewhat close, but the closest gives me the correct shape of output without the correct content. It looks like this:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&0"
}
}
}
]
But it results in an output like this:
[ {
"0" : "Company",
"1" : "Retail Cost",
"2" : "Percentage"
}, {
"0" : "ABC",
"1" : "5,368.11",
"2" : "17.09%"
}, {
"0" : "DEF",
"1" : "101.47",
"2" : "0.32%"
}, {
"0" : "GHI",
"1" : "83.79",
"2" : "0.27%"
} ]
Which clearly has the wrong object name and it has 1 too many elements in the output.
Can do it, but wow it is hard to read / looks like terrible regex
Spec
[
{
// this does most of the work, but producs an output
// array with a null in the Zeroth space.
"operation": "shift",
"spec": {
// match the first item in the outer array and do
// nothing with it, because it is just "header" data
// e.g. "Company", "Retail Cost", "Percentage".
// we need to reference it, but not pass it thru
"0": null,
//
// loop over all the rest of the items in the outer array
"*": {
// this is rather confusing
// "*" means match the array indices of the innner array
// and we will write the value at that index "ABC" etc
// to "[&1].#(2,[0].[&])"
// "[&1]" means make the ouput be an array, and at index
// &1, which is the index of the outer array we are
// currently in.
// Then "lookup the key" (Company, Retail Cost) using
// #(2,[0].[&])
// Which is go back up the tree to the root, then
// come back down into the first item of the outer array
// and Index it by the by the array index of the current
// inner array that we are at.
"*": "[&1].#(2,[0].[&])"
}
}
},
{
// We know the first item in the array will be null / junk,
// because the first item in the input array was "header" info.
// So we match the first item, and then accumulate everything
// into a new array
"operation": "shift",
"spec": {
"0": null,
"*": "[]"
}
}
]

Extracting a subset of attributes with JSONPath

I have this JSON code:
{
"A": {
"AB": [{
"ABA": "0",
"ABB": "1",
"ABC": "2"
}]
}
}
I need to use a JSONPath expression that returns that JSON with only ABA and ABC attributes. Something like:
{
"A": {
"AB": [{
"ABA": "0",
"ABC": "2"
}]
}
}
So far I manage to extract either one or all attributes. For example
$.A.AB[*]
or
$.A.AB[*].ABA
Is there a way to extract only two?
Thanks
This will work using the Jayway implementation (Java):
$.A.AB[*]['ABB', 'ABA']
and the result for your input would be:
[
{
"ABB" : "1",
"ABA" : "0"
}
]
You can Compare different providers here:
http://jsonpath.herokuapp.com/

Builtin Query Capabilities in JSON using Javascript or JQuery

I am looking query the JSON data based on some where conditions for ex. list the person names whose cell no is 777-777-7777.
pl let me know what are query capabilities in JSON.
var json = [{
"name": "senthil",
"Phoneno": [{
"Home": "111-111-1111"
},
{
"Cell": "222-222-2222"
},
{
"Office": "333-333-3333"
}
],
"City": "Hartford"
},
{
"name": "kumar",
"Phoneno": [{
"Home": "444-555-6666"
},
{
"Cell": "777-777-7777"
},
{
"Office": "888-888-8888"
}
],
"City": "Austin"
},
];
var people = json.filter(function(el)
{
return el.Phoneno.some(function(number)
{
return number.Cell == "777-777-7777";
});
});
This uses the Array.filter and Array.some functions from ECMAScript 5. filter returns an array of the elements that pass a test. some returns true if any element in an array passes the test.
For browsers that don't support it, you can use the code at MDC.
This will almost certainly not be faster than the obvious for-loop approach.
As a note, if every person can have up to one phone number per type, a simpler representation would be:
"Phoneno": { "Home": "111-111-1111",
"Cell": "222-222-2222",
"Office": "333-333-3333"
}
Also, JSON technically refers to the text representation, not actual JavaScript objects.