JSON display method - html

I have scenario where I could use your help for the best practice and efficient code.
I have a JSON Array like below
[
{
"Date": "2014-07-16",
"DiscPoint": "Description 1",
"DisBy": "Person 1"
},
{
"Date": "2014-07-16",
"DiscPoint": "Description 2",
"DisBy": "Person 2"
},
{
"Date": "2014-07-16",
"DiscPoint": "Description 3",
"DisBy": "Person 3"
}
]
How do I omit the first element while iterating the JSONArray, Please note I have to omit it only after the first iteration.
I need to display the date for every new date. Please help me out in this scenario.

In $.each() function just add if-statement which will be skipping first index, which is equal to 0.
$.each(yourArray, function(index, value) {
if (index != 0) {
// do something
}
});

Related

Is there a way where all the values from a JSON column can be used as query params to fetch values in db?

I have a lookup table with columns old_item_code, description, new_item_code.
Now my items is an array of objects.
"items":[
{
"code": "OLDCODE1",
"description": "sample description1",
"value": "Sample value1"
},
{
"code": "OLDCODE2",
"description": "Sample Description2",
"value": "Sample Value 2"
}
]
Now I want my items array to be replaced with new item code which has to be queried from the lookup table(postgres).
I want my final result to be like
"items": [
{
"NEWCODE1": "Sample Value 1",
"NewCODE2": "Sample Value 2"
}
]
In PSQL, using where with array will get you the new code. Use a map to get its values.

Copy value from grandchildren if it exists, use null otherwise

Given the JSON:
{
"id": 1,
"coding": [{
"code": 1234,
"system": "target"
}, {
"code": 5678,
"system": "other"
}]
}
I can select the value of "code" where the "system" is "target", thus:
{id: .id} + {"code": .coding[]? | select(.system=="target").code}
To produce:
{
"id": 1,
"code": 1234
}
But if the object whose "system" value is "target" does not exist in the array, thus:
{
"id": 1,
"coding": [{
"code": 5678,
"system": "other"
}]
}
I want the following result:
{
"id": 1,
"code": null
}
However, my above jq produces an empty object. How can I achieve what I want?
The select built-in yields empty unless at least one of its inputs meets the given criteria, and empty consumes almost anything around itself. Hence the empty result.
Instead, use the first built-in for alternating between the code value from the object where system is target, and null. This also covers some other cases you didn't mention explicitly.
{ id, code: first((.coding[]? | select(.system == "target") .code), null) }
Online demo

Split a string and trim a known prefix from each part in a complex JSON structure

I'm dealing with a fairly complex JSON-structure in which a single entry needs to be edited in several places. For example:
[
{
"name": "test 1",
"stuff": {
"properties": {
"id": 0,
"stuff_list": [
{
"entryId": 1,
"description": "- item 1\n- item 2\n- item 3"
},
{
"entryId": 2,
"description": "- item 1\n- item 2\n- item 3"
}
]
}
}
},
{
"name": "test 2",
"stuff": {
"properties": {
"id": 1,
"stuff_list": [
{
"entryId": 1,
"description": null
},
{
"entryId": 2,
"description": "- item 1\n- item 2\n- item 3"
}
]
}
}
}
]
Here I would like to edit each "description"-element: The string needs to be split at each \n and the substrings "^\n?-\s" of each resulting array element need to be removed. So it should result in:
{
"entryId": 1,
"description": ["item 1", "item 2", "item 3"]
}
My first approach is:
jq '.[].stuff.properties.stuff_list[].description | split("\n")' the_file.json
but that's not working in the first place becaue of the null values that can occur at some places. So now I wonder: how can I achieve what I want?
An alternate version using split() on the \n and trimming string - on the left, would be to do
.[].stuff.properties.stuff_list[].description |=
if . != null then
split("\n") | map(ltrimstr("- "))
else
.
end
jqplay - Demo

Remove parent elements with certain key-value pairs using JQ

I need to remove elements from a json file based on certain key values. Here is the file I am trying to process.
{
"element1": "Test Element 1",
"element2": {
"tags": "internal",
"data": {
"data1": "Test Data 1",
"data2": "Test Data 2"
}
},
"element3": {
"function1": {
"tags": [
"new",
"internal"
]
},
"data3": "Test Data 3",
"data4": "Test Data 4"
},
"element4": {
"function2": {
"tags": "new"
},
"data5": "Test Data 5"
}
}
I want to remove all elements that have a "tag" with value "internal". So the result should look like this:
{
"element1": "Test Element 1",
"element4": {
"function2": {
"tags": "new"
},
"data5": "Test Data 5"
}
}
I tried various approaches but I just don't get it done using jq. Any ideas? Thanks.
Just to add some more complexity. Let's assume the json is:
{
"element1": "Test Element 1",
"element2": {
"tags": "internal",
"data": {
"data1": "Test Data 1",
"data2": "Test Data 2"
}
},
"element3": {
"function1": {
"tags": [
"new",
"internal"
]
},
"data3": "Test Data 3",
"data4": "Test Data 4"
},
"element4": {
"function2": {
"tags": "new"
},
"data5": "Test Data 5"
},
"structure1" : {
"substructure1": {
"element5": "Test Element 5",
"element6": {
"tags": "internal",
"data6": "Test Data 6"
}
}
}
}
and I want to get
{
"element1": "Test Element 1",
"element4": {
"function2": {
"tags": "new"
},
"data5": "Test Data 5"
},
"structure1" : {
"substructure1": {
"element5": "Test Element 5",
}
}
}
Not easy, finding elements which have a tags key somewhere whose value is either the string internal, or an array of which an element is the string internal in a reliable way is only possible with a complex boolean expression as below.
Once found, deleting them can be done using the del built-in.
del(.[] | first(select(recurse
| objects
| has("tags") and (.tags
| . == "internal" or (
type == "array" and index("internal")
)
)
)))
Online demo
I think I figured out how to also solve the more complex case. I am now running:
walk(if type == "object" and has("tags") and (.tags | . == "internal" or (type == "array" and index("internal"))) then del(.) else . end) | delpaths([paths as $path | select(getpath($path) == null) | $path])
This will remove all elements that contain 'internal' as 'tag'.
The following solution is written with a helper function for clarity. The helper function uses any for efficiency and is defined so as to add a dash of generality.
To understand the solution, it will be helpful to know about with_entries and the infix // operator, both of which are explained in the jq manual.
# Does the incoming JSON value contain an object which has a .tags
# value that is equal to $value or to an array containing $value ?
def hasTag($value):
any(.. | select(type=="object") | .tags;
. == $value or (type == "array" and index($value)));
Assuming the top-level JSON entity is a JSON object, we can now simply write:
with_entries( select( .value | hasTag("internal") | not) )

Dropdown filter is not working in angularjs

I have created a drop down filter in AngularJs. Please see the link.
http://plnkr.co/edit/c5Hrqfv1eA5qfQpkYR41?p=preview
It is working fine, but I want to replace my current JSON to a new one.
$scope.data={
"language": "en",
"0": {
"id": "2222",
"deck": [
{
"id": "2421",
"level": "5",
"name": "Plaza Deck 5"
},
{
"id": "2433",
"level": "6",
"name": "Fiesta Deck 6"
},
{
"id": "2442",
"level": "7",
"name": "Promenade Deck 7"
}
]
}
}
I want to change with the above JSON.
Please help.
Thanks.
I don't know why you are switching from simpler array to complex array.
But here is your solution
HTML
<select ng-model="filterDeck1.deckDetail1" ng-options="deck.name for deck in data1['0'].deck">
</select>
JavaScript
//filter for new json structure
$scope.customDeck1 = function (data) {
if (data.id === $scope.filterDeck1.deckDetail1.id) {
return true;
} else {
return false;
}
};
You can move above filter code from controller to filter, that would be more convenient way to do it.
Here is Working Fiddle
Hope this could help you. Thanks.