Remove the parent element from JSON reply - javascript

I have a route used for AJAX calls. It gets items from a DB and returns a JSON array.
I'm using:
return reply({
myArray
}).code(200);
Everything works but my output in the browser is:
{
"myArray":[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
}
But what I need is:
{
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
}
Very basically, I need to get rid the "myArray" parent element and leave just the array there. It looks like a simple task but I can't find documentation or samples anywhere.
Thanks,
Marco

This:
{
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
}
is invalid JSON notation. Within curly braces, you should have key-value pairs where keys are strings and values are valid JSON values (strings, numbers, booleans, null, arrays, or objects).
Perhaps what you expect is just the array:
[
{
"_id":"1",
"name":"Asd1"
},
{
"_id":"2",
"name":"Asd2"
}
]
which is valid JSON. In this case, you may simply send it to your reply function:
return reply(myArray).code(200);
For more info on JSON notation, see the article on MDN and play with JSON.stringify to develop better intuition on when the JSON you see is valid or not.

Related

How to parse an string into more workable key-values?

So I have the following response:
{
"errors": [
{
"errorKey": "ERROR_NO_DELIVERY_OPTIONS",
"errorParameters": "[{\"errorMessage\":\"ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW\",\"partNumbers\":[\"19308033\",\"19114798\"]},{\"errorMessage\":\"Pickup At Seller not available for these orderItemIds\",\"orderItemIds\":[\"10315031\",\"10315032\"],\"availableShipModeId\":\"13201\"}]",
"errorMessage": "ERROR_NO_DELIVERY_OPTIONS",
"errorCode": "ERROR_NO_DELIVERY_OPTIONS"
}
]
}
Unfortunately, I'm not sure how to work with the value of "errorParameters" since it just a string and not a simple key-value like the others. How would I extract all the information so I can work with it. A co-woker mentioned parsing it but not sure what he meant by that and how. Below is a more readable value. I'm working with javascript.
[
{
"errorMessage": "ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW",
"partNumbers":
[
19308033,
19114798
]
},
{
"errorMessage": "No Shipmodes Available for these orderItemsIds",
"orderItemIds": [
10315031,
10315032
]
}
]
You will need to use JSON.parse to transform JSON strings into a JS Object.
You'll need to do this in your code. I.E.
data.errors.forEach(e => console.log(JSON.parse(e.errorParameters)))
You can use just JSON.parse() function, which changes string JSON representation into a JavaScript object.
const parsedErrorParameters = JSON.parse(data.errorParameters);
console.log(parsedErrorParameters[0].errorMessage); // ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Accessing JSON value with unique named subarrays

I want to sort a JSON array based on time value in a subarray with the key names of the subarrays being named uniquely.
I'm searching for the method to access key, value update_time of every element in Products so I can use that value in a sorting script.
I have tried sorting the array but can not determine how to access the key, values of the subarrays
Expected behavior should be that every unique_keyname_# element is available for sorting and is sorted for further processing in JavaScript. Ultimately with the newest unique_keyname_# as the first element in a list, based on the update_time key.
var obj = {
"company": {
"department_1": {
"Products": {
"unique_keyname_1": {
"product_owner": "co-worker-1",
"update_time": "unix_timestamp_1"
},
"unique_keyname_5": {
"product_owner": "co-worker-4",
"update_time": "unix_timestamp_45"
},
"unique_keyname_8": {
"product_owner": "co-worker-2",
"update_time": "unix_timestamp_5"
}
}
},
"department_2": {
"Products": {
"unique_keyname_3": {
"product_owner": "co-worker-1",
"update_time": "unix_timestamp_21"
},
"unique_keyname_6": {
"product_owner": "co-worker-2",
"update_time": "unix_timestamp_7"
},
"unique_keyname_4": {
"product_owner": "co-worker-3",
"update_time": "unix_timestamp_75"
}
}
}
}
}
I solved the issue by writing an intermediate script in python which makes the API response a valid array. From there it was fairly easy to sort the data.
Thanks for the replies confirming the data itself was deliverd to me in an inappropriate format!
regards
In your example, there are no arrays.
Anyway, in Javascript you can access a node using . like:
obj.company.department_1.Products.unique_keyname_1
Or using [] which gives you more freedom to use costume fields
obj["company"]["department_1"]["Products"]["unique_keyname_1"]
// can also be more dynamic as:
obj["company"]["department_"+ department_counter]["Products"]["unique_keyname_" + keyname_counter]
Is there a possibility that you will change the structure of your JSON? to make it more manangeable ?
if so, i would recommend the folowing structure:
var products = [
{
department: 'SomeDepartment',
productName: 'Something',
productOwner: 'Someone',
update_time: 'Sometime'
}
]
Then you can sort the array easy using Array.sort()
for the sort topic use this : Sort array of objects by string property value

JSON parsing with multiple data

Jsonlint display that this JSON object is valid:
[{"obj":{"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]","path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"}}]
I'm trying to access to markers with the k, B and path elements but it's always set to undefined. Here is my code:
try {
var jsonData = JSON.parse(myJson);
console.log(jsonData.obj[0].markers[0].k);
}
catch (e) {
console.error("Parsing error:", e);
}
Can someone tell me how to access to the element of my JSON object properly? Thanks for the help.
Something must have gone wrong in creating this string. Yes, it's valid JSON, but it has a different format than you think, because you escape control characters like " and [, ].
Try this string instead:
[
{
"obj":{
"markers":[
{
"k":47.040182144806664,
"B":0.52734375
},
{
"k":50.90303283111257,
"B":10.37109375
},
{
"k":52.53627304145945,
"B":-1.7578125
},
{
"k":41.77131167976406,
"B":-6.591796875
}
],
"path":[
[
47.040182144806664,
0.52734375
],
[
50.90303283111257,
10.37109375
],
[
52.53627304145945,
-1.7578125
],
[
41.77131167976406,
-6.591796875
]
]
}
}
]
as opposed to your string:
[
{
"obj":{
"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
"path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"
}
}
]
You have two issues from what I can tell:
One, yes it is valid Json but the marker and path object values are enclosed in string quotes:
"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
"path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"
what you rather want is:
"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],
"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]
But given the above, also not escape the k and b object names for markers \"k\" should be "k":
So the completed edited JSON would look like this:
[{"obj":{"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]}}]

Parsing a JSON object-within-an-object in javascript

I have a JSON object that looks like this:
var json = {
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": [ {... // contains lots objects with the data I'd like to access } ]
As noted above, I want to access the product array of objects. I can't seem to do this though. I've tried:
console.log(json['cj-api']['products'][0]['product']);
But I get typeError: Cannot read property 'products' of undefined.
What's the correct way to access the array of product (note, singular product, not products). This data is coming from an external source so I can't alter the hyphen in cj-api.
EDIT: Here's what the raw console log of json looks like:
{"cj-api":{"products":[{"$":{"total-matched":"231746","records-returned":"999","page-number":"1"},"product":[{ << lots of data in here>>
EDIT 2: To further clarify, I got this object by running JSON.stringify(result) after I put some XML into XML2js.
i have tried the following JSON structure:
var json = {
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": [
{
"a": "a",
"b": "b",
"c": "c"
}
]
}
]
}
};
with the log statement as:
console.log(json['cj-api']['products'][0]['product']);
And result is as follows:
[Object { a="a", b="b", c="c"}]
Well your way of accessing json is absolutely correct. This is for debugging. Try
console.log(json['cj-api']);
console.log(json['cj-api']['products']);
console.log(json['cj-api']['products'][0]);
console.log(json['cj-api']['products'][0]['product']);
Which ever line returns undefined means your json is broken there.
If this doesn't work then you need to check for other similar keys. Maybe they value you are trying to find is actually undefined.
Maybe you are trying to loop. If you are then check for the condition if (JSONStructure[key]==undefined) console.log("Undefined at position ..."). That is the only way if you have valid JSON.
typeError: Cannot read property 'products' of undefined means that json exists, but json['cj-api'] is undefined. If you are sure that you are using the right variable name, I think this might be a scope issue, where you are using an other variable than you intend to. json might be the json string, instead of the array-like object. Try renaming your variable and see if you still get this problem. Otherwise the string is not automatically parsed for you and you'll have to parse it with JSON.parse( ... ).
Edit:
var json = '{ "me": "be an evil string" }';
console.log( json ); //'{ "me": "be an evil string" }'
console.log( json['me'] ); //undefined
console.log( JSON.parse( json )['me'] ); // 'be an evil string'
Since your question is missing the last
}
]
}
}
and others here changed your example and made it work, did you try to correct it?
If not then I suggest you or the dataprovider correct the structure of the reply.
I have tried below json structure
var json={
"cj-api": {
"products": [
{
"$": {
"total-matched": "231746",
"records-returned": "999",
"page-number": "1"
},
"product": []
}
]
}
}
now json['cj-api']['products'][0]['product'] will work

Regex for parsing single key: values out of JSON in Javascript

I'm trying to see if it's possible to lookup individual keys out of a JSON string in Javascript and return it's Value with Regex. Sort of like building a JSON search tool.
Imagine the following JSON
"{
"Name": "Humpty",
"Age": "18",
"Siblings" : ["Dracula", "Snow White", "Merlin"],
"Posts": [
{
"Title": "How I fell",
"Comments": [
{
"User":"Fairy God Mother",
"Comment": "Ha, can't say I didn't see it coming"
}
]
}
]
}"
I want to be able to search through the JSON string and only pull out individual properties.
lets assume it's a function already, it would look something like.
function getPropFromJSON(prop, JSONString){
// Obviously this regex will only match Keys that have
// String Values.
var exp = new RegExp("\""+prop+"\"\:[^\,\}]*");
return JSONString.match(exp)[0].replace("\""+prop+"\":","");
}
It would return the substring of the Value for the Key.
e.g.
getPropFromJSON("Comments")
> "[
{
"User":"Fairy God Mother",
"Comment": "Ha, can't say I didn't see it coming"
}
]"
If your wondering why I want to do this instead of using JSON.parse(), I'm building a JSON document store around localStorage. localStorage only supports key/value pairs, so I'm storing a JSON string of the entire Document in a unique Key. I want to be able to run a query on the documents, ideally without the overhead of JSON.parsing() the entire Collection of Documents then recursing over the Keys/nested Keys to find a match.
I'm not the best at regex so I don't know how to do this, or if it's even possible with regex alone. This is only an experiment to find out if it's possible. Any other ideas as a solution would be appreciated.
I would strongly discourage you from doing this. JSON is not a regular language as clearly stated here: https://cstheory.stackexchange.com/questions/3987/is-json-a-regular-language
To quote from the above post:
For example, consider an array of arrays of arrays:
[ [ [ 1, 2], [2, 3] ] , [ [ 3, 4], [ 4, 5] ] ]
Clearly you couldn't parse that with true regular expressions.
I'd recommend converting your JSON to an object (JSON.parse) & implementing a find function to traverse the structure.
Other than that, you can take a look at guts of Douglas Crockford's json2.js parse method. Perhaps an altered version would allow you to search through the JSON string & just return the particular object you were looking for without converting the entire structure to an object. This is only useful if you never retrieve any other data from your JSON. If you do, you might as well have converted the whole thing to begin with.
EDIT
Just to further show how Regex breaks down, here's a regex that attempts to parse JSON
If you plug it into http://regexpal.com/ with "Dot Matches All" checked. You'll find that it can match some elements nicely like:
Regex
"Comments"[ :]+((?=\[)\[[^]]*\]|(?=\{)\{[^\}]*\}|\"[^"]*\")
JSON Matched
"Comments": [
{
"User":"Fairy God Mother",
"Comment": "Ha, can't say I didn't see it coming"
}
]
Regex
"Name"[ :]+((?=\[)\[[^]]*\]|(?=\{)\{[^\}]*\}|\"[^"]*\")
JSON Matched
"Name": "Humpty"
However as soon as you start querying for the higher structures like "Posts", which has nested arrays, you'll find that you cannot correctly return the structure since the regex does not have context of which "]" is the designated end of the structure.
Regex
"Posts"[ :]+((?=\[)\[[^]]*\]|(?=\{)\{[^\}]*\}|\"[^"]*\")
JSON Matched
"Posts": [
{
"Title": "How I fell",
"Comments": [
{
"User":"Fairy God Mother",
"Comment": "Ha, can't say I didn't see it coming"
}
]
\{|\}|\[|\]|,|:|(\\-)?\\d+(\\.\\d+)?|".+?"
You can use the following regex and iterate with a match over all tokens of a json. You can tokenize the JSON, but the parsing part has to be implemented by you.
Since you're using JavaScript as I assume from the tags, your best way to encode the JSON stays JSON.parse().
I'm almost 10 years late to the party, but I came up with this.
Not tested in crazier JSONs than this, but it solves my use cases.
const obj1 = {
id: 1,
'name.1': '123',
address: {
'address.1': 'Chicken Dinner Road, 69',
'address.2': 'Psycho lane, 666',
},
'age.1': {
'thisIsSomeCrazyJson.3': 10,
age: 50,
},
types: [
{
id: 22,
'name.name': '123',
typeOption: {
id: 1,
'whoTFWroteThisJSON.2': '123',
},
},
{
id: 32,
'name.1': '123',
},
],
};
const obj2 = {
Name: 'Humpty',
Age: '18',
Siblings: ['Dracula', 'Snow White', 'Merlin'],
Posts: [
{
Title: 'How I fell',
Comments: [
{
'User': 'Fairy God Mother',
'Comment': "Ha, can't say I didn't see it coming",
},
],
},
],
};
function matchKeyDeep(input, pattern) {
return Object.entries(input).reduce((nextInput, [key, value]) => {
const isMatch = pattern.test(key);
if (Array.isArray(value)) {
const arrValue = value;
let nextValue = arrValue.map((arrItem) => {
if (typeof arrItem === 'object') {
return matchKeyDeep(arrItem, pattern);
}
return arrItem;
});
if (!isMatch && Array.isArray(nextValue)) {
nextValue = nextValue.filter((v) => (typeof v === 'object' && v !== null));
if (nextValue.length === 0) return nextInput;
}
nextInput[key] = nextValue;
return nextInput;
}
if (typeof value === 'object') {
const recurse = matchKeyDeep(value, pattern);
if (!isMatch && Object.keys(recurse).length === 0) {
return nextInput;
}
nextInput[key] = recurse;
return nextInput;
}
if (isMatch) {
nextInput[key] = value;
}
return nextInput;
}, {});
}
const res = matchKeyDeep(obj1, /\.\d/);
const res2 = matchKeyDeep(obj2, /Comment/);
console.log(res);
console.log(res2);
First, stringify the JSON object. Then, you need to store the starts and lengths of the matched substrings. For example:
"matched".search("ch") // yields 3
For a JSON string, this works exactly the same (unless you are searching explicitly for commas and curly brackets in which case I'd recommend some prior transform of your JSON object before performing regex (i.e. think :, {, }).
Next, you need to reconstruct the JSON object. The algorithm I authored does this by detecting JSON syntax by recursively going backwards from the match index. For instance, the pseudo code might look as follows:
find the next key preceding the match index, call this theKey
then find the number of all occurrences of this key preceding theKey, call this theNumber
using the number of occurrences of all keys with same name as theKey up to position of theKey, traverse the object until keys named theKey has been discovered theNumber times
return this object called parentChain
With this information, it is possible to use regex to filter a JSON object to return the key, the value, and the parent object chain.
You can see the library and code I authored at http://json.spiritway.co/

Categories

Resources