Returning data from parsed xml to JSON - json

I am attempting to display some data from JSON that contains parsed XML, in an angular application, and I am stuck.
I can extract the other fields like so {{object.articleId}}. How can I extract child data from the content object? For example, if I need to extract the "default-locale"? I would like to be able to do {{object.content.default-locale}} but this is not working obviously.
[
{
"articleId": "393800",
"classNameId": 0,
"classPK": 0,
"companyId": 10157,
"content": "{\"root\":{\"default-locale\":\"en_US\",\"dynamic-element\":[{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"[\\\"General News\\\"]\",\"language-id\":\"en_US\"},\"name\":\"Top_News\",\"type\":\"radio\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"_All\",\"language-id\":\"en_US\"},\"name\":\"Location\",\"type\":\"list\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"CTC\",\"language-id\":\"en_US\"},\"name\":\"Business_Unit\",\"type\":\"list\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"hfhfahfcaf;\",\"language-id\":\"en_US\"},\"name\":\"Subtitle\",\"type\":\"text\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"dhfhfowejfwkfpw\",\"language-id\":\"en_US\"},\"name\":\"Intro_Text\",\"type\":\"text\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"1430697600000\",\"language-id\":\"en_US\"},\"name\":\"News_Date\",\"type\":\"ddm-date\"},{\"index\":0,\"index-type\":\"\",\"dynamic-content\":{\"content\":\"/image/journal/article?img_id=393803&t=1430750130449\",\"id\":393803,\"language-id\":\"en_US\"},\"name\":\"News_Image\",\"type\":\"image\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"flkdsjfldsjffa\",\"language-id\":\"en_US\"},\"name\":\"Freeform\",\"type\":\"text_area\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"content\":\"test\",\"language-id\":\"en_US\"},\"name\":\"News_Tag_1\",\"type\":\"text\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"language-id\":\"en_US\"},\"name\":\"News_Tag_2\",\"type\":\"text\"},{\"index\":0,\"index-type\":\"keyword\",\"dynamic-content\":{\"language-id\":\"en_US\"},\"name\":\"News_Tag_3\",\"type\":\"text\"}],\"available-locales\":\"en_US\"}}",
"createDate": 1430750130000,
"description": "",
"descriptionCurrentValue": "",
"displayDate": 1430749860000,
"expirationDate": 1430836418000,
"folderId": 236650,
"groupId": 10184,
"id": 393801,
"indexable": true,
"layoutUuid": "",
"modifiedDate": 1430836418000,
"resourcePrimKey": 393802,
"reviewDate": null,
"smallImage": false,
"smallImageId": 393804,
"smallImageURL": "",
"status": 3,
"statusByUserId": 10439,
"statusByUserName": "Admin",
"statusDate": 1430836418000,
"structureId": "236652",
"templateId": "236654",
"title": "<?xml version='1.0' encoding='UTF-8'?><root available-locales=\"en_US\" default-locale=\"en_US\"><Title language-id=\"en_US\">Test</Title></root>",
"titleCurrentValue": "Test",
"treePath": "/236650/",
"type": "general",
"urlTitle": "test",
"userId": 10439,
"userName": "Admin",
"uuid": "f06c946e-6efb-4a28-81f3-2a78e1a20814",
"version": 1.0
}
]
Thanks in advance for your assistance

content is actually a json string.
You need to loop over that data when you receive it and set:
object.content = angular.fromJson(object.content);
Perhaps this could be also cleaned up at source if you control the api that is sending it

Related

Why is JSON being detected as empty?

So I have a JSON file I got from Postman which is returning as an empty object. This is how I'm reading it.
import regscooter from './json_files/reginald_griffin_scooter.json'
const scoot = regscooter;
const CustomerPage = () => {...}
reginald_griffin_scooter.json
{
"success": true,
"result": {
"id": "hhhhhhhhhh",
"model": "V1 Scooter",
"name": "hhhhhhhhhh",
"status": "active",
"availabilityStatus": "not-available",
"availabilityTrackingOn": true,
"serial": "hhhhhhhhhhhh",
"createdByUser": "hhhhhhhhK",
"createdByUsername": "hhhhhhhh",
"subAssets": [
"F0lOjWBAnG"
],
"parts": [
"hhhhhhhh"
],
"assignedCustomers": [
"hhhhhhhhh"
],
"createdAt": "2019-12-03T21:47:26.218Z",
"updatedAt": "2020-06-26T22:05:54.526Z",
"customFieldsAsset": [
{
"id": "hhhhhhh",
"name": "MAC",
"value": "hhhhhhhh",
"asset": "hhhhhhhhhh",
"user": "hhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.732Z"
},
{
"id": "hhhhhhhh",
"name": "IMEI",
"value": "hhhhhhh",
"asset": "hhhhhhh",
"user": "hhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.834Z"
},
{
"id": "hhhhhhhhh",
"name": "Key Number",
"value": "NA",
"asset": "hhhhhhhhh",
"user": "hhhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.911Z"
}
]
}
}
The error is that "const scoot" is being shown as an empty object {}. I made sure to save a ton of times everywhere. I am able to read through the imported JSON file in other variables in similar ways, so I don't know why I can't parse this one. I just want to access the JSON object inside this. Also I omitted some information with hhhhh because of confidentiality.
EDIT: The code works, but it still has a red line beneath result when I do:
const scoot = regscooter.result.id;
It would be much more effective if you will provide an example in codesandbox or so.
However at first look it might be a parser issue ( maybe you are using Webpack with missing configuration for parsing files with json extension ), meaning we need more information to provide you with a full answer ( maybe solution ? ).
Have you tried to do the next:
const scoot = require('./json_files/reginald_griffin_scooter.json');

How to handle assertion property content for differenrt responses?

I would like to handle assertion property content according to below responses, for both success and failure.
Success response:
{
"task": "userLogin",
"response": {
"code": 0,
"status": "success",
"error_message": "",
"success_message": "",
"data": {
"userId": "65",
"username": "indian",
"email": "test#gmail.com",
"token": "b0aef6139ffdc1041e01f7587a0dcf61",
"userType": "trial",
"profile_picture": "test.png"
}
}
}
Failure response: will have only the data node missing, and remaining nodes will be present.
"data":
{
"userId": "65",
"username": "indian",
"email": "test#gmail.com",
"token": "b0aef6139ffdc1041e01f7587a0dcf61",
"userType": "trial",
"profile_picture": "test.png"
}
You can use a very simple XPath assertion, keeping in mind that internally SoapUI converts everything into XML representation.
XPath:
exists(//*:data)
Expected:
true
Update based on your comments.
Your original requirement was not clear. The node is present it is just empty. In that case the XPath assertion would be:
empty(//*:data)
Expected:
false
Handy XPath reference.

Getting the value of an image field in Plone 3.x with FSS

I'm trying to migrate an old Plone 3.3 site that uses
FileSystemStorage using Mikko's Simple JSON export script.
Everything runs fine, except for the absence of a value on the image fields, which are returned as empty strings (''):
[
...
{
"allowDiscussion": false,
"contributors": [],
"creation_date": "2009-11-04T15:15:36-02:00",
"creators": [
"johndoe"
],
"description": "",
"effectiveDate": null,
"excludeFromNav": false,
"expirationDate": null,
"id": "banner_vertical.jpg",
"image": "",
"language": "",
"location": "",
"modification_date": "2009-11-04T15:15:37-02:00",
"portal_type": "Image",
"relatedItems": [],
"rights": "",
"subject": [],
"title": "Banner vertical",
"urlLegend": "http://"
},
...
]
Any hint?
(iw.fss version used is 2.8.0rc5).
FSS was not known or used by the site for which the script has been made.
You might want to retrofit convert() and other methods to support your use case.

xPath of JSON response in soapUI

can anyone help in finding xpath of 'tenantId' which is in JSON response given below
{
"statusCode": "200",
"errorParams": null,
"payLoad": {
"UGX:XCD": {
"id": 42802,
"fromCurrency": {
"tenantId": "2a610af6-2e42-4a78-8e04-98755e2c6113",
"code": "UGX",
"name": "Ugandan Shilling",
"isoCode": "UGX",
"active": true,
"enabled": true,
"flagModifiedName": false,
"erpId": null,
"createdOn": 1427155200000,
"fieldLibraryStatus": "DRAFT",
"userId": null
},
"toCurrency": {
"tenantId": "2a610af6-2e42-4a78-8e04-98755e2c6113",
"code": "XCD",
"name": "East Caribbean Dollar",
"isoCode": "XCD",
"active": true,
"enabled": true,
"flagModifiedName": true,
"erpId": null,
"createdOn": 1427155200000,
"fieldLibraryStatus": "DRAFT",
"userId": "c7f68b05-044d-44e1-8fc5-0f97a4ece91b"
},
"rate": 33,
"inverseRate": 0.0303,
"createdOn": 1434025926983,
"modifiedOn": 1434326400000,
"active": true,
"purpose": "cmd",
"enabled": true,
"tenantFlag": true,
"erpId": null
}
How to traverse till 'tenantId' ??
Thanks in Advance.
SoapUI internally converts almost anything to XML. You will have to use the ResponseAsXml property. In the GUI, if you click on the XML tab, you can see this representation.
Something like the following should work:
${test_step#ResponseAsXml#//*:fromCurrency/*:tenantId}
${test_step#ResponseAsXml#//*:toCurrency/*:tenantId}
If you want to handle this using groovy, then extend SOAPUI with GSON.jar library provided by Google.
this library specially written to handle the JSON objects.
With this you can rip every individual objects and its attribute-values easily how complex may the JSON object be, a nice add-on to work with.

WordPress jSON API how to return data with jquery?

I enabled json API successfully in a WordPress blog but I have hard time returning data with jQuery.
http://example.com/?json=1&count=1&include=title
Here is the json format. Ideally I'd like to append in a div called #homeblog, the last post title, linked to the post itself.
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{
"id": 1,
"type": "post",
"slug": "hello-world",
"url": "http:\/\/localhost\/wordpress\/?p=1",
"title": "Hello world!",
"title_plain": "Hello world!",
"content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
"excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
"date": "2009-11-11 12:50:19",
"modified": "2009-11-11 12:50:19",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "",
"url": "",
"description": ""
},
"comments": [
{
"id": 1,
"name": "Mr WordPress",
"url": "http:\/\/wordpress.org\/",
"date": "2009-11-11 12:50:19",
"content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.<\/p>\n",
"parent": 0
}
],
"comment_count": 1,
"comment_status": "open"
}
]
}
If you want to append some data returned by the JSON api to a page you would need something like:
$(document).ready(function(){
$.getJSON("http://example.com/?json=1&count=1",function(data) {
$('body').append('<div id="homeblog">'+data.posts[0].title+'</div>');
}
I have removed the include=title part of the json call because that excludes everything else. You are using getJSON to return the post data, then it is just a case of constructing your html and append ing it to the page somewhere (in my case right onto the <body> tag)
EDIT: there is a wordpress stackexchange for wordpress questions. you will have more luck asking questions like this over there.
(added a right bracket)