如何将两个JSON数组合并为指定格式的二维数组?
Merge Two JSON Arrays into Target 2D Array
Looks like you need to pair up the label values from the categories array with the corresponding value values from the data array into a 2D array. Let's walk through how to do this properly, including fixing the syntax errors in your original JSON.
Step 1: Fix JSON Syntax Errors
First, notice your original JSON snippets have a small syntax issue: the keys categories and data are missing the opening double quote. We'll correct that first so the JSON can be parsed successfully.
Step 2: Implementation in JavaScript
Here's a straightforward way to do this in JavaScript:
// Corrected JSON strings const categoriesJson = '{"categories":[{"label":"2018-05-24T12:04:01.000Z"},{"label":"2018-05-24T12:05:03.000Z"},{"label":"2018-05-24T12:08:08.000Z"},{"label":"2018-05-24T12:09:10.000Z"},{"label":"2018-05-24T12:11:14.000Z"},{"label":"2018-05-24T12:12:15.000Z"},{"label":"2018-05-24T12:13:17.000Z"},{"label":"2018-05-24T12:15:21.000Z"},{"label":"2018-05-24T12:16:22.000Z"},{"label":"2018-05-24T12:17:24.000Z"},{"label":"2018-05-24T12:17:26.000Z"},{"label":"2018-05-24T12:18:28.000Z"},{"label":"2018-05-24T12:19:30.000Z"},{"label":"2018-05-24T12:20:31.000Z"},{"label":"2018-05-24T12:21:33.000Z"}]}'; const dataJson = '{"data":[{"value":23.44},{"value":23.43},{"value":23.42},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.37},{"value":23.36},{"value":23.35},{"value":23.38},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.39},{"value":23.37},{"value":23.35},{"value":23.34},{"value":23.35},{"value":23.34},{"value":23.32},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.37},{"value":23.3},{"value":23.35},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.33},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35}]}'; // Parse JSON into arrays const categories = JSON.parse(categoriesJson).categories; const data = JSON.parse(dataJson).data; // Merge into target 2D array, handling length differences const mergedArray = categories.slice(0, Math.min(categories.length, data.length)).map((category, index) => { return [category.label, data[index].value]; }); console.log(mergedArray);
Explanation:
- We first parse the corrected JSON to get the
categoriesanddataarrays. Math.min(categories.length, data.length)ensures we only iterate over indices that exist in both arrays, preventingundefinedvalues if one array is longer than the other.- The
mapmethod pairs eachlabelwith the correspondingvalueat the same index.
Step 3: Implementation in Python
If you're working with Python, here's an equivalent solution:
import json # Corrected JSON strings categories_json = '''{"categories":[{"label":"2018-05-24T12:04:01.000Z"},{"label":"2018-05-24T12:05:03.000Z"},{"label":"2018-05-24T12:08:08.000Z"},{"label":"2018-05-24T12:09:10.000Z"},{"label":"2018-05-24T12:11:14.000Z"},{"label":"2018-05-24T12:12:15.000Z"},{"label":"2018-05-24T12:13:17.000Z"},{"label":"2018-05-24T12:15:21.000Z"},{"label":"2018-05-24T12:16:22.000Z"},{"label":"2018-05-24T12:17:24.000Z"},{"label":"2018-05-24T12:17:26.000Z"},{"label":"2018-05-24T12:18:28.000Z"},{"label":"2018-05-24T12:19:30.000Z"},{"label":"2018-05-24T12:20:31.000Z"},{"label":"2018-05-24T12:21:33.000Z"}]}''' data_json = '''{"data":[{"value":23.44},{"value":23.43},{"value":23.42},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.37},{"value":23.36},{"value":23.35},{"value":23.38},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.39},{"value":23.37},{"value":23.35},{"value":23.34},{"value":23.35},{"value":23.34},{"value":23.32},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.37},{"value":23.3},{"value":23.35},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.33},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35}]}''' # Parse JSON categories = json.loads(categories_json)['categories'] data = json.loads(data_json)['data'] # Merge arrays merged_array = [] min_length = min(len(categories), len(data)) for i in range(min_length): merged_array.append([categories[i]['label'], data[i]['value']]) print(merged_array)
Key Notes:
- Always validate your JSON syntax first—missing quotes are a common pitfall that will break parsing.
- Handling array length differences is important to avoid runtime errors.
内容的提问来源于stack exchange,提问作者newcoder
相关产品推荐
相关产品推荐

