Extract an element value in json using jsonpath - javascript

Following is my json data
{
"primary": [
{
"secondary": {
"name": {
"fullname": "fullname1"
},
"alias": "alias1"
},
"reference": "reference1"
},
{
"secondary": {
"name": {
"fullname": "fullname2"
},
"alias": "alias2"
},
"reference": "reference2"
}
]
}
Now I want to extract "alias" value based on condition based fullname value from this json data using jsonpath.
I am using following expression but failed to parse result
$.primary[*].secondary[?(#.name.fullname == "fullname1")].alias

Your JSON is malformed, if it really looks like you posted - you won't be able to use JSON Extractor, you will have to switch to Regular Expression Extractor.
Valid JSON would be something like:
{
"primary": [
{
"secondary": {
"name": {
"fullname": "fullname1"
},
"alias": "alias1"
},
"reference": "reference1"
},
{
"secondary": {
"name": {
"fullname": "fullname2"
},
"alias": "alias2"
},
"reference": "reference2"
},
{
"type": "online"
}
]
}
And if it is correct your JsonPath expression works fine as it evidenced by JsonPath Tester mode of the View Results Tree listener:

Related

How to have v-for loop nested in an other v-for loop in vuejs working?

I have a types variable that is an array of objects. Each object has 2 properties name and content.
The second property content is an array of object with only one property : name.
When displayed in the template with {{types}} I see this:
[ { "name": "Base", "content": [ { "name": "Base (Lager/Pilsner)" }, { "name": "Base (Pale)" }, { "name": "Base (Pale Ale)" }, { "name": "Base (Wheat)" }, { "name": "Base (Rye" }, { "name": "Base (Wheat)" } ] },
{ "name": "Kilned", "content": [ { "name": "Munich" }, { "name": "Vienna" }, { "name": "Aromatic" }, { "name": "Amber|Biscuit|Victory" }, { "name": "Brown Malt" } ] },
{ "name": "Stewed", "content": [ { "name": "Caramel|Crystal" }, { "name": "Dextrin" }, { "name": "Special Belge" }, { "name": "Honey Malt" } ] },
{ "name": "Roasted/Torrefied", "content": [ { "name": "Pale Chocolate" }, { "name": "Chocolate" }, { "name": "Black Wheat" }, { "name": "Roast Barley" }, null, { "name": "Roast Rye" }, { "name": "BLack Malt" } ] },
{ "name": "Others", "content": [ { "name": "Acidulated" } ] } ]
Here is my template
<div class="h-3/4 overflow-auto">
<div v-for="(group,index) in types">
<FermentableTypeItem
#updateModel="updateModel"
:key="index"
:type_name="group.name"
:group_name="group.name"
:state="group.state"
></FermentableTypeItem>
{{group.content}}
<FermentableTypeItem
v-for=" (t,index) in group.content"
#updateModel="updateModel"
:key="index"
:type_name="t.name"
:group_name="group.name"
></FermentableTypeItem>
</div>
</div>
As you can see I want to add a special [1] FermentableTypeItem for each first level element and then loop on this first level element 's content property to add a list of normal [2] FermentableTypeItem.
Note 1: special means that the group_name and the type_name are
identical
Note 2: normal means the the group_name and the type_name are
different
It works and display the various FermentableTypeItem s but only when I don't use the t variable in the second loop
If I use it, the app crashes saying the t is undefined.
Could somebody help me fixing this error ? May be it's obvious but I cannot see what is wrong.
There is null content object present. So remove null content from response your or check null v-if="t != null" like below.
<div v-for=" (t,index) in group.content" :index="index">
<FermentableTypeItem v-if="t != null"
#updateModel="updateModel"
:key="index"
:type_name="t.name"
:group_name="group.name"
></FermentableTypeItem>
</div>

How to modify an object

I am trying to "push" information inside the object based on certain conditions.
I need to insert/push/assign more information within the property "materials" but I am not sure how to do this.
This is the structure of my current object:
const MYOBJECT = {
"status": "Work",
"present": true,
"materials": [
{
"link1": {
"url": "www.google.com",
}
},
{
"driveFile": {
"driveFile": {
"id": "xyz123456fghtfsdag"
},
"shareMode": "VIEW"
}
},
],
"dueDate": {
"day": resource.date_day,
"month": resource.date_month,
"year": resource.date_year
},
}
I am trying to add/push, if a condition is met, the variables shown below ("additional1" & "additional2") inside the object property "materials" without replacing/deleting any other information within the object:
var additional1 = { "link2": { "url": "www.yahoo.com", } }
var additional2 = { "link3": { "url": "www.bing.com", } }
I am looking for the END result to look like this (assuming the "if statements" / conditions are met):
const MYOBJECT = {
"status": "Work",
"present": true,
"materials": [
{
"link1": {
"url": "www.google.com",
}
},
// Insert variable addition1
{
"link2": {
"url": "www.yahoo.com",
}
},
// Insert variable addition2
{
"link3": {
"url": "www.bing.com",
}
},
{
"driveFile": {
"driveFile": {
"id": "xyz123456fghtfsdag"
},
"shareMode": "VIEW"
}
},
],
"dueDate": {
"day": resource.date_day,
"month": resource.date_month,
"year": resource.date_year
},
}
materials is an array, so you can add new members there using the push() method.
The end result would look something like this:
if (conditionIsMet) {
MYOBJECT.materials.push(additional);
}
Please note, that the push() method adds element to the end of the array, so if you want your results to reside in the middle of the array (like you did in your example) you'll need to specify additional logic for this.

Creating Complex JSON Object using JavaScript Code

I am bit new to JSON world. And I have to use JavaScript to create following type of JSON structure. Not sure how to achieve this. Tried with following code, but unable to add second element("12101") as well as people to JSON Structure is where I am struggling.
var chat = {};
chat = {"101":{}};
chat["101"].people= {};
chat["101"].people = {"L0b12leL-Ar9GYKoAAAC":{}};
chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"vikram#qech.com"};
chat["101"].room= {};
JSON structure to achieve
{
"101": {
"people": {
"L0b12leL-Ar9GYKoAAAC": {
"name": "vikram#qtech.com",
"inroom": "f787f316-6424-491b-b779-cfc396f0f8a1",
"owns": "f787f316-6424-491b-b779-cfc396f0f8a1",
"countrycode": "in",
"device": "desktop",
"roomname": "R1"
},
"qKCglYWI1hRhZUZCAAAD": {
"name": "Ishim",
"inroom": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"owns": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"roomname": "Ra"
}
},
"room": {
"f787f316-6424-491b-b779-cfc396f0f8a1": {
"name": "R1",
"id": "f787f316-6424-491b-b779-cfc396f0f8a1",
"owner": "L0b12leL-Ar9GYKoAAAC",
"people": [
"L0b12leL-Ar9GYKoAAAC"
],
"status": "available"
},
"2e52905d-951c-4990-b9b7-2f3fc0602922": {
"name": "Ra",
"id": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"owner": "qKCglYWI1hRhZUZCAAAD",
"people": [
"qKCglYWI1hRhZUZCAAAD"
],
"status": "available"
}
}
},
"12101": {
"people": {
"K-Ar9GYKoAAAC": {
"name": "Rahul.com",
"inroom": "f787f316-6424-491b-b779-cfc396f0f8a1",
"owns": "f787f316-6424-491b-b779-cfc396f0f8a1",
"countrycode": "in",
"device": "desktop",
"roomname": "R1"
},
"I1hRhZUZCAAAD": {
"name": "Vipul",
"inroom": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"owns": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"roomname": "Ra"
}
},
"room": {
"b779-cfc396f0f8a1": {
"name": "Rahul-R1",
"id": "f787f316-6424-491b-b779-cfc396f0f8a1",
"owner": "L0b12leL-Ar9GYKoAAAC",
"people": [
"L0b12leL-Ar9GYKoAAAC"
],
"status": "available"
},
"b9b7-2f3fc0602922": {
"name": "Vipul-Room1",
"id": "2e52905d-951c-4990-b9b7-2f3fc0602922",
"owner": "qKCglYWI1hRhZUZCAAAD",
"people": [
"qKCglYWI1hRhZUZCAAAD"
],
"status": "available"
}
}
}
}
This is invalid because the property name contains dashes.
chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"vikram#qech.com"};
To access it correctly, put it in quotes
chat["101"].people["L0b12leL-Ar9GYKoAAAC"] = {"name":"vikram#qech.com"};
Use bracket notation as a property accessor like this:
chat["12101"].people = {};
chat["101"].people["L0b12leL-Ar9GYKoAAAC"] = {"name":"vikram#qech.com"};
With it, it’s just a routine piece of work. It probably didn’t work right away since dot notation property access requires a valid identifier name. With bracket notation, you can use any string like "L0b12leL-Ar9GYKoAAAC".
Also note that in JSON, anything works as a property name too, as long as it is put in quotes. {"L0b12leL-Ar9GYKoAC":true} is as valid as {"💖":true}.

How to manipulate this JSON using Javascript

I am getting this response from an API:
{
"statuses": {
"status": [
{
"name": "Member",
"id": "1"
},
{
"name": "Attender",
"id": "3"
},
{
"name": "Child",
"id": "4"
}
]
}
}
But I need to somehow flatten the response to be this:
{
"name": "Member",
"id": "1"
},
{
"name": "Attender",
"id": "3"
},
{
"name": "Child",
"id": "4"
}
How can I do that using Javascript?
var response = {
"statuses": {
"status": [
{
"name": "Member",
"id": "1"
},
{
"name": "Attender",
"id": "3"
},
{
"name": "Child",
"id": "4"
}
]
}
}
var statusObj = response.statuses.status;
$('#result').text('First name is: ' + statusObj[0].name)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="result"/>
You can do JSON.parse(str) and then you you just take the data from status[x]
If you really want to keep it as a string you can do
var content = str.match(/\[(.*?)\]/);
In fact, you just need to retrieve by response.statuses.status from your Javascript object.
But , If you needed to convert json to javascript object,
please use JSON.parse(your json response) method using JSON.js.
Download the JSON.js from https://github.com/douglascrockford/JSON-js

JSON response with header and entries

I have a system that makes JSON responses like this. This is a list to use in a table. I have never seen this way of writing the field names first and then the data in another set?
Is this way of writing JSON data called something special?
I cannot figure how to read the response in my javascript/jQuery, does anyone have an example or google hint?
(anyone know if "jtable" can read it?)
"WBEMLIST": {
"header": [
{
"name": "K9LBNR"
},
{
"name": "K9BEM"
},
{
"name": "CBRUGER"
},
{
"name": "CDATO"
},
{
"name": "CTID"
},
{
"name": "UBRUGER"
},
{
"name": "UDATO"
},
{
"name": "UTID"
}
],
"entries": [
[
1,
"Albert Einstein described\\the \"ork of theorists as making",
"WEBUSERFAU",
20140620,
114036,
"",
0,
0
],
[
3,
"of reality is incomplete. Thus the concept that Einstein",
"WEBUSERFAU",
20140620,
114036,
"",
0,
0
],
}
EDIT: I can easily read a list in javascript that i wrote like this in JSON, but not the above!:
{
"Bildlist": [
{
"K8LBNR": "1",
"K8IMAG": "/HB0WKL3Q.PDF",
"K8TYPE": "D",
"K8LINKB": "",
"CBRUGER": "WEBUSER",
"CDATO": "2014.06.03",
"CTID": "13.54.55"
},
{
"K8LBNR": "1",
"K8IMAG": "/HB0WKL3Q.PDF",
"K8TYPE": "D",
"K8LINKB": "",
"CBRUGER": "WEBUSER",
"CDATO": "2014.06.03",
"CTID": "13.54.55"
}
]
}

Categories

Resources