Parse data with no header
I have an API which pulls in data
{"name":["details"],"name":["details"],"name":["details"],"name":["details"],..
If I MagicParse this I just get ONE long row with these details.
What I am after is 2 columns of Name and Details. I have parsed the data and added the columns titles but how do I reference the data?
for(var i=0 ; i<jsonRes.length ; i++) { datagrid.addCell(jsonRes[i].?); datagrid.addCell(jsonRes[i].?); datagrid.endRow(); }
Best Answer
-
Can you reformat the JSON incoming, before you handle it?
Loop through each object in the array, extracting the
details
from the"name"
key and creating a new object with the key"details"
associated with the value.// Simulating the incoming invalid JSON (you receive this from the API)
var invalidJson = [
{ "name": ["details1"] },
{ "name": ["details2"] },
{ "name": ["details3"] },
{ "name": ["details4"] }
];
// Initialize an array to store the reformatted data
var validJsonArray = [];
// Loop through each entry and reformat it
for (var i = 0; i < invalidJson.length; i++) {
validJsonArray.push({
"name": "name", // This assumes "name" is constant
"details": invalidJson[i].name[0] // Extracting the detail from each object
});
}
// The validJsonArray now contains the reformatted data
console.log(validJsonArray);After reformatting:
[
{ "name": "name", "details": "details1" },
{ "name": "name", "details": "details2" },
{ "name": "name", "details": "details3" },
{ "name": "name", "details": "details4" }
]for (var i = 0; i < validJsonArray.length; i++) {
datagrid.addCell(validJsonArray[i].name); // Adds "name" to the first column
datagrid.addCell(validJsonArray[i].details); // Adds the corresponding details to the second column
datagrid.endRow(); // End the current row
}If you regularly receive this malformed JSON from the API, you can wrap the reformatting code in a function and call it each time you receive the data.
function reformatJsonData(invalidJson) {
var validJsonArray = [];
for (var i = 0; i < invalidJson.length; i++) {
validJsonArray.push({
"name": "name",
"details": invalidJson[i].name[0]
});
}
return validJsonArray;
}
// Usage
var reformattedData = reformatJsonData(invalidJson);
// Process the reformatted data as needed** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
Answers
-
The issue in your JSON code seems to be the repeated "name" keys without nesting. To properly reference the data in your loop, you should ensure that each entry in the array is an object with distinct keys for "name" and "details".
JSON[
{"name": "details1", "detail": "detail1"},
{"name": "details2", "detail": "detail2"},
{"name": "details3", "detail": "detail3"}
]for(var i = 0; i < jsonRes.length; i++) {
datagrid.addCell(jsonRes[i].name);
datagrid.addCell(jsonRes[i].detail);
datagrid.endRow();
}** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Hi, Yes that's the problem, without any key I cannot reference the data. This is data coming from a web service so I have no control over the format.
0 -
Given a structure:
{
"name": ["details1"],
"name": ["details2"],
"name": ["details3"],
"name": ["details4"]
}This JSON structure is not valid because JSON keys must be unique within the same object.
If your data is like this:
[
{"name": "details1"},
{"name": "details2"},
{"name": "details3"},
{"name": "details4"}
]
You can probably loop through each object and extract thename
value as shown before.var jsonRes = {
"name": ["details1", "details2", "details3", "details4"]
};
for (var i = 0; i < jsonRes.name.length; i++) {
datagrid.addCell("name"); // Add the name or label (if it's the same for all)
datagrid.addCell(jsonRes.name[i]); // Add the corresponding detail value
datagrid.endRow();
}** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Unfortunately that is not how my data is though :(
0 -
That's too bad. And you can't go back to the source of the API code and talk to them about it?
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
Unfortunately not, its a global service used by 1000's of customers 'as is'.
0 -
Can you reformat the JSON incoming, before you handle it?
Loop through each object in the array, extracting the
details
from the"name"
key and creating a new object with the key"details"
associated with the value.// Simulating the incoming invalid JSON (you receive this from the API)
var invalidJson = [
{ "name": ["details1"] },
{ "name": ["details2"] },
{ "name": ["details3"] },
{ "name": ["details4"] }
];
// Initialize an array to store the reformatted data
var validJsonArray = [];
// Loop through each entry and reformat it
for (var i = 0; i < invalidJson.length; i++) {
validJsonArray.push({
"name": "name", // This assumes "name" is constant
"details": invalidJson[i].name[0] // Extracting the detail from each object
});
}
// The validJsonArray now contains the reformatted data
console.log(validJsonArray);After reformatting:
[
{ "name": "name", "details": "details1" },
{ "name": "name", "details": "details2" },
{ "name": "name", "details": "details3" },
{ "name": "name", "details": "details4" }
]for (var i = 0; i < validJsonArray.length; i++) {
datagrid.addCell(validJsonArray[i].name); // Adds "name" to the first column
datagrid.addCell(validJsonArray[i].details); // Adds the corresponding details to the second column
datagrid.endRow(); // End the current row
}If you regularly receive this malformed JSON from the API, you can wrap the reformatting code in a function and call it each time you receive the data.
function reformatJsonData(invalidJson) {
var validJsonArray = [];
for (var i = 0; i < invalidJson.length; i++) {
validJsonArray.push({
"name": "name",
"details": invalidJson[i].name[0]
});
}
return validJsonArray;
}
// Usage
var reformattedData = reformatJsonData(invalidJson);
// Process the reformatted data as needed** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 753 Beast Mode
- 61 App Studio
- 41 Variables
- 692 Automate
- 177 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive