a need parse son from alfresco API, but I have problem with two slashes in URL model address. How can I get value from "prejimka.stavHodnoceni"? Can anyone help me ?
My javascript:
var nazevSlozky = null;
var result = remote.call("/api/metadata?nodeRef=workspace://SpacesStore/7d2eab73-9500-406a-bdb0-40209924b2d2");
var json = JSON.parse(result);
var nazevSlozky = json.properties.{http://ourFirm.cz/model/someFirm/3.0.}prejimka.stavHodnoceni;
JSON:
My json
Try json.properties['sf:prejimka.stavHodnoceni'] where "sf" is the model abbreviation.
Also, I don't think you want to use punctuation in your property names. I'm not sure it is expressly forbidden but it seems like a bad idea.
Related
I have this response from an API:
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
how can i select the pix_expiration_date key??? I have tried:
payment_object['acquirer_response']['pix_expiration_date']
it doesnt work and it returns to me:
TypeError: string indices must be integers
Since you’re looking for json
Here’s a code you can start with, keep the good work :)
import json
#importing json
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
###
dic=json.loads(payment_object["acquirer_response"])
#loading the object
dic['pix_expiration_date']
#it will return 2022-04-28T13:46:01.000Z
Are you the owner of the api? If yes:
Edit the response, because it’s sending an string not an object/dictionary
If No:
Convert the string to an dictionary, like if you using python try json library
Your json is not valid. I don't know what language are you using, this code works for javascript, you can easily translate it to another language
var payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'};
var s=JSON.stringify(payment_object).replaceAll("\\","").replaceAll("\"{","{").replaceAll("}\"","}");
var paymentObject=JSON.parse(s);
console.log(paymentObject['acquirer_response']['pix_expiration_date']); // 2022-04-28T13:46:01.000Z
I am trying to get the sess_id from the html response in postman.
this is my test.
var test3 = cheerio(pm.response.text());
var variabile = test3.find('[pageId="/Security/SelectPaymentMethod?sess_id=SbyYFpsCefH3hVD3KNB7JwJ7VsYxdNBA1PKDbDJugmNZZyNZIJZeVdAb9H1neJfarNelwg6qXZx&paymentMethodCode=VEC&paymentMethodTypeCode=QTA&environment=Test"]').val();
console.log(variabile('sess_id').val());
HTML
<link accesskey="1" pageId="/Security/SelectPaymentMethod?sess_id=HnapitQl8k9CBhiCkeaKPQP681rb8kFkHmgZKYRbxNw9SxMMcUtwpsKn5bA2s2drwwBzv3S3W1T&paymentMethodCode=VEC&paymentMethodTypeCode=QTA&environment=Test">Verve eCash - Active</link>
in my console I get the response
TypeError | variable is not a function
This is a different and horrible way you could get that value:
let resData = xml2Json(pm.response.text());
let filteredData = resData.link.$.pageId.split('=')
console.log(filteredData[1].split('&')[0])
It's using xml2Json and passing that the response as text, it's then traversing down the data and splitting the pageId value. Not clean at all and I wouldn't use this anywhere other than figuring out a better solution but it's proving it can be done.
Thanks Danny for the effort.
Though I figured out another way.
I converted the xml to JSON first of all and made the test in JSON
let resp = xml2Json (pm.response.text()),
ussdsessionId = resp.page.navigation.link['2'].$.pageId;
console.log({ ussdsessionId });
pm.environment.set("sessionBank", ussdsessionId);
I am using asp.net webapi and returning json response. One of the property in the json response has got a path field. When i check in Console.WriteLine i get the values without additional double slashes. But the json response in postman contains extra double slashes.
Expected:
\\test.com\cax\mef\160704\160704Z003.abc
But in the json response the output i am getting is:
"path": "\\\\test.com\\cax\\mef\\160704\\160704Z004.abc"
And my json settings in webapi.config:
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Can anyone help me what is the problem and how to solve this?
Why i am getting extra double slashes?
Thanks
The added slashes are their as Escape characters.
This Could be caused when you asign the URL to the Variable before you return the response via the Webapi. Eg:
xObj.path = urlPath;
----------------
xObj.path = urlPath.ToString();
It All Depends on where and how you Assigned them.. If they are coming for a Database they could have also Been Stored Like that.
it could also just be the IDE showing it to you Like that. This Often Just Adds the Escape characters to the interpretation of the value.. but when the value is used they are no longer present.
this is how you parse the JSON
var jsonResponse = '{ "firstName":"John" , "lastName":"Doe", "path" : "/test/test/adasdasd.com" }';
var obj = JSON.parse(jsonResponse);
console.log(obj);
You Will note that my Example doesn't have the double slashes. That is because if you use the response directly it wont have any either.
I have a JSON file that contains what I believe to be a correct JSON string:
{"title": "exampleTitle", "tipTitle": "exampleTipTitle", "tip": "exampleTip"}
I'm trying to parse said file and take out the 3 values then store them in variables, however currently, it parses each individual character as a separate object, therefore:
JSONobj[1] = "
and so on. Assuming that currentLocation = the directory location of the json file.
Code
var jsonLocation = currentLocation + "json.txt";
var request = new XMLHttpRequest();
request.open("GET", jsonLocation, false);
request.send(null);
var returnValue = request.responseText;
var JSONobj = JSON.parse(JSON.stringify(returnValue));
var headerTitle = JSONobj[0];
A few clarifications, the stringify is in because it was throwing an unexpected token error. I've tried changing the file tile to .json instead but that also makes no difference. "It also gives off a XMLHttpRequest on the main thread is deprecated" but I'm not particularly sure how to solve that issue. Any help would be appreciated.
var returnValue = request.responseText;
Here returnValue is a string of JSON.
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
var JSONobj = JSON.parse(JSON.stringify(returnValue));
Here you convert the string of JSON to JSON. So you have a JSON string representing a string, and that string is a representation of a data structure in JSON.
"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"
Then you parse it and convert it back to the original string of JSON.
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
So you end up back where you start.
Just don't use JSON.stringify here, and you'll convert your JSON to a JavaScript object:
var javascript_object = JSON.parse(returnValue);
Then you have an object, but it doesn't have a 0 property so it doesn't make sense to access it with javascript_object[0]. The properties have names, such as javascript_object.title.
Your JSON doesn't describe an array, so indexing into it with an index like 0 doesn't make sense. Your JSON describes an object, which will have properties with the names title, tipTitle, and tip.
Additionally, you're overdoing your parsing: You just want to parse, not stringify (which is the opposite of parsing):
var JSONobj = JSON.parse(returnValue);
So:
var JSONobj = JSON.parse(returnValue);
var headerTitle = JSONobj.title;
console.log(headerTitle); // "exampleTitle"
Side note: By the time you've assigned it to the variable you've called JSONobj, it isn't JSON anymore, it's just a normal JavaScript object, so that name is a bit misleading. If you're writing source code, and you're not dealing with a string, you're not dealing with JSON anymore. :-)
My web service is returning below mentioned output as JSON,
{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}
But I doubt cause I am not able to parse it.
If it is correct pls tell me how to parse it? I want to bind it to chart.
Full code
var retVal = '{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917},{"DailyTimeStamp":"Nov 02, 2013","Performance":119542}]}';
alert('Before parsing ' + retVal);
var passValue = JSON.parse(retVal);
alert('After parsing Count ' + passValue.count);
last alert give output as After parsing Count undefined.
The simplest way to parse your json in javascript is that you store it inside a variable and can proceed like this:
var jsonString=JSON.parse(yourJsonString);
then you can fetch each key and their value accordingly.
Just check your json on JsonEditoronline.org for a better way of reading this.
This is sample of how I read my json in javascript:
success:function(responseText)
{
alert(responseText);
return false;
var object= JSON.parse(responseText);
var response= object.response;
var div=$('<div></div>');
var docs= response.docs;
where my json was something like this:
{"response":{"docs":""}}
Hope it helps.
I parsed your json using javascript and was able to parse it using :
var myString='{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}';
var jsonString=JSON.parse(myString);
alert("---------==="+jsonString);
var fetch= jsonString.FetchSitePerformanceAutoResult;
alert("llllllll -- "+fetch.length);
var time= fetch[0].DailyTimeStamp;
alert(time);
To tell if JSON is correct, you can use http://jsonlint.com/ Your JSON is correct. To parse it, use the .net JSON Deserializer http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx or JSON.NET Also see here for some discussion: Does .NET 4 have a built-in JSON serializer/deserializer?
EDIT: as you want Javascript, use JSON.parse. See some discussion here: http://www.json.org/js.html which also explains why you should not use eval(json), which does also work, but is considered a security risk.
Yes your json output is correct and you can check yourself here is the Link, this will help you convert your json to c# class. Hope this helps.
That is a valid JSON String. You can check it at http://jsonlint.com/
It is legal - php json_decode('{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}'); parses it fine.